Last active
May 22, 2025 07:13
-
-
Save arofiqimaulana/42e98baf13e3cd09c4cb8bbc257857e6 to your computer and use it in GitHub Desktop.
FindDateAndConvert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def convertDate(text): | |
| import re | |
| pattern = r'\b(\d{1,2})\s+(jan|january|januari|feb|februari|february|mar|march|maret|apr|april|may|mei|jun|juni|jul|juli|july|aug|august|agustus|sep|september|sept|okt|oct|oktober|october|nov|november|dec|des|desember|december)\s+(\d{4})\b' | |
| matches = re.findall(pattern, text, flags=re.IGNORECASE) | |
| if len(matches) > 0: | |
| day = matches[0][0] | |
| month = matches[0][1].lower() # normalize ke lowercase | |
| year = matches[0][2] | |
| if month in ('jan', 'january', 'januari'): | |
| month_num = 1 | |
| elif month in ('feb', 'februari', 'february'): | |
| month_num = 2 | |
| elif month in ('mar', 'march', 'maret'): | |
| month_num = 3 | |
| elif month in ('apr', 'april'): | |
| month_num = 4 | |
| elif month in ('may', 'mei'): | |
| month_num = 5 | |
| elif month in ('jun', 'juni'): | |
| month_num = 6 | |
| elif month in ('jul', 'juli', 'july'): | |
| month_num = 7 | |
| elif month in ('aug', 'august', 'agustus'): | |
| month_num = 8 | |
| elif month in ('sep', 'sept', 'september'): | |
| month_num = 9 | |
| elif month in ('okt', 'oct', 'oktober', 'october'): | |
| month_num = 10 | |
| elif month in ('nov', 'november'): | |
| month_num = 11 | |
| elif month in ('dec', 'des', 'desember', 'december'): | |
| month_num = 12 | |
| else: | |
| return None # bulan tidak dikenali | |
| # Format day dan month dengan 2 digit | |
| day_str = day.zfill(2) | |
| month_str = str(month_num).zfill(2) | |
| return f"{year}-{month_str}-{day_str}" | |
| return None # kalau tidak ada tanggal cocok | |
| def extract_and_format_date(text): | |
| import re | |
| # Regex untuk kedua pola tanggal: | |
| # 1) yyyy-mm-dd atau yyyy/mm/dd | |
| # 2) dd/mm/yyyy | |
| pattern = re.compile(r'(\d{4})[-/](\d{1,2})[-/](\d{1,2})|(\d{1,2})/(\d{1,2})/(\d{4})') | |
| match = pattern.search(text) | |
| if not match: | |
| return None | |
| if match.group(1): # pola yyyy-mm-dd atau yyyy/mm/dd | |
| year = match.group(1) | |
| month = match.group(2).zfill(2) | |
| day = match.group(3).zfill(2) | |
| else: # pola dd/mm/yyyy | |
| day = match.group(4).zfill(2) | |
| month = match.group(5).zfill(2) | |
| year = match.group(6) | |
| return f"{year}-{month}-{day}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment