Skip to content

Instantly share code, notes, and snippets.

@dzakyputra
Created October 11, 2019 03:06
Show Gist options
  • Save dzakyputra/bee40a298f0ac8d8ce14655249d63987 to your computer and use it in GitHub Desktop.
Save dzakyputra/bee40a298f0ac8d8ce14655249d63987 to your computer and use it in GitHub Desktop.
# Search the date of birth
def search_dob(text):
# Define the pattern to get dates (20-07-2018, 20 07 2018, 20-07 2018, 20 07-2018)
reg_dob = '(\d{2}[\/ -]\d{2}[\/ -]\d{2,4})'
# Find all strings that match
dates = re.findall(reg_dob, text)
# We iterate through the results and convert it into datetime format
for i in range(len(dates)):
date = re.sub('[- ]', '', dates[i])
try:
dates[i] = datetime.strptime(date, '%d%m%Y')
except:
dates[i] = datetime.strptime('01013000', '%d%m%Y')
# We take the smallest dates in a KTP, beacuse it must be his/her birth date
date = min(dates)
return datetime.strftime(date, '%d/%m/%Y')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment