2

Is there a way where to guess date format of a string and convert other dates to the same format as the given string (e.g. YYYYMMDD)?

For example:

# Recognize the format as YYYYMMDD
date1 = '20221111'

# Recognize the format as YYYY-MM-DD and converts to YYYYMMDD
date2 = '2022-11-12'
accdias
  • 5,160
  • 3
  • 19
  • 31
ben
  • 1,404
  • 8
  • 25
  • 43
  • what is same original here ? – Surjit Samra Jan 07 '23 at 14:49
  • This might help https://stackoverflow.com/questions/29295402/format-date-without-dash – smitty_werbenjagermanjensen Jan 07 '23 at 14:50
  • 2
    There is no way to do this reliably because human conventions are ambiguous. Is "121110" November 10, 2012 ; November 12, 2010; October 11, 2012. December 11, 2010; or something else? – tripleee Jan 07 '23 at 14:59
  • I'm voting to close this as duplicate of [datetime from string in Python, best-guessing string format](https://stackoverflow.com/questions/9507648/datetime-from-string-in-python-best-guessing-string-format) – mkrieger1 Jan 07 '23 at 19:59

3 Answers3

1

You can use dateutil.parser.parse(), which can parse dates dynamically.

For example:

from dateutil import parser

date1 = '20221113'
date2 = '2022-11-13'

print(parser.parse(date1).strftime('%d-%m-%Y'))
print(parser.parse(date2).strftime('%Y%m%d'))

#13-11-2022
#20221113
accdias
  • 5,160
  • 3
  • 19
  • 31
tomerar
  • 805
  • 5
  • 10
0

I don't quite understand what you mean by "guessing the date format" what you mean is changing the default way Python works with dates? Because that is difficult if not impossible. Secondly if you want to format that text why not use datetime.

from datetime import datetime
datetime.strptime("20221112", '%Y%m%d') # datetime.datetime(2022, 12, 11, 0, 0)
# Or you can also try
datetime.strptime("20221211", '%Y%d%m') # datetime.datetime(2022, 11, 12, 0, 0)

if you are bothered by using datetime.strptime you can use it in a function

def format_time(time_str, format='%Y%m%d'):
  return datetime.strptime(time_str, format)
print(format_time("20231011")) # 2023-10-11 00:00:00

As a resource I leave you this to help you with formats Python Strptime

Of course, if you don't know how the sample data comes, you will have to interpret it by default because it is impossible to know how to interpret it. I would personally use YYYYMMDD

0

Your best bet might be infer_datetime_format from pandas

df = pd.DataFrame({'dates':['20221111','2022-11-12']})
df
Out[28]: 
        dates
0    20221111
1  2022-11-12

Let pandas do guess date format

df.dates = pd.to_datetime(df.dates,infer_datetime_format=True)

df.dates
Out[42]: 
0   2022-11-11
1   2022-11-12
Name: dates, dtype: datetime64[ns]

You can format it in whatever suits you.

df.dates.dt.strftime('%Y%m%d')
Out[43]: 
0    20221111
1    20221112
Name: dates, dtype: object
Surjit Samra
  • 4,614
  • 1
  • 26
  • 36