Python: Converting between datetime and string

This blog talks about converting datetime between string and datetime with python. The principal python package we will use is datetime and dateutil.

Converting between datetime and string is a subject that we usually need to handle during data processing. In this blog, I’ll talk about how to convert datetime to string and inverse, with following date format:

  • YYYY-MM-DD
  • YY-MM-DD
  • MM-DD-YYYY
  • MM-DD-YY
  • DD-MM-YYYY
  • DD-MM-YY
  • YYYY/MM/DD
  • YY/MM/DD
  • MM/DD/YYYY
  • MM/DD/YY
  • DD/MM/YYYY
  • DD/MM/YY
  • Human-intelligible date

Converting from datetime to string

YYYY-MM-DD

>>> import datetime
>>> dt_stamp = datetime.datetime(2019, 1, 9)
>>> type(dt_stamp)
datetime.datetime
>>> str(dt_stamp)
'2019-01-09 00:00:00'

You can convert datetime objects and pandas Timestamp object to string with str or the strftime method:

>>> dt_stamp.strftime('%Y-%m-%d')
'2019-01-09'
>>> dt_stamp.strftime('%F')
'2019-01-09'

%Y means four-digit year, %m presents two-digit month, %d describes two-digit day, %F is the shortcut for %Y-%m-%d.

YY-MM-DD

>>> dt_stamp.strftime('%y-%m-%d')
'19-01-09'

%y means two-digit year.

MM-DD-YYYY

>>> dt_stamp.strftime('%m-%d-%Y')
'01-09-2019'

MM-DD-YY

>>> dt_stamp.strftime('%m-%d-%y')
'01-09-19'

DD-MM-YYYY

>>> dt_stamp.strftime('%d-%m-%Y')
'09-01-2019'

DD-MM-YY

>>> dt_stamp.strftime('%d-%m-%y')
'09-01-19'

YYYY/MM/DD

>>> dt_stamp.strftime('%Y/%m/%d')
'2019/01/09'

YY/MM/DD

>>> dt_stamp.strftime('%y/%m/%d')
'19/01/09'

MM/DD/YYYY

>>> dt_stamp.strftime('%m/%d/%Y')
'01/09/2019'

MM/DD/YY

>>> dt_stamp.strftime('%m/%d/%y')
'01/09/19'
>>> dt_stamp.strftime('%D')
'01/09/19'

%D is the shortcut for %m/%d/%y.

DD/MM/YYYY

>>> dt_stamp.strftime('%d/%m/%Y')
'09/01/2019'

DD/MM/YY

>>> dt_stamp.strftime('%d/%m/%y')
'09/01/19'

Converting from string to datetime

YYYY-MM-DD

>>> str_stamp = '2019-01-09'
>>> datetime.datetime.strptime(str_stamp, '%Y-%m-%d')
datetime.datetime(2019, 1, 9, 0, 0)

datetime.datetime.strptime is a good way to parse a date with a known format. However, it can be a bit annoying to have to write a format spec each time, especially for common date formats. In this case, you can use the parser.parse method in the third-party dateutil package.

>>> from dateutil.parser import parse
>>> parse(str_stamp)
datetime.datetime(2019, 1, 9, 0, 0)

YY-MM-DD

>>> datetime.datetime.strptime('19-01-09', '%y-%m-%d')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('19-01-09', yearfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

MM-DD-YYYY

>>> datetime.datetime.strptime('01-09-2019', '%m-%d-%Y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('01-09-2019', yearfirst=False, dayfirst=False)
datetime.datetime(2019, 1, 9, 0, 0)

MM-DD-YY

>>> datetime.datetime.strptime('01-09-19', '%m-%d-%y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('01-09-19', yearfirst=False, dayfirst=False)
datetime.datetime(2019, 1, 9, 0, 0)

DD-MM-YYYY

>>> datetime.datetime.strptime('09-01-2019', '%d-%m-%Y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('09-01-2019', dayfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

DD-MM-YY

>>> datetime.datetime.strptime('09-01-19', '%d-%m-%y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('09-01-19', dayfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

YYYY/MM/DD

>>> datetime.datetime.strptime('2019/01/09', '%Y/%m/%d')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('2019/01/09')
datetime.datetime(2019, 1, 9, 0, 0)

YY/MM/DD

>>> datetime.datetime.strptime('19/01/09', '%y/%m/%d')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('19/01/09', yearfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

MM/DD/YYYY

>>> datetime.datetime.strptime('01/09/2019', '%m/%d/%Y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('01/09/2019', yearfirst=False, dayfirst=False)
datetime.datetime(2019, 1, 9, 0, 0)

MM/DD/YY

>>> datetime.datetime.strptime('01/09/19', '%m/%d/%y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('01/09/19', yearfirst=False, dayfirst=False)
datetime.datetime(2019, 1, 9, 0, 0)

DD/MM/YYYY

>>> datetime.datetime.strptime('09/01/2019', '%d/%m/%Y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('09/01/2019', dayfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

DD/MM/YY

>>> datetime.datetime.strptime('09/01/19', '%d/%m/%y')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('09/01/19', dayfirst=True)
datetime.datetime(2019, 1, 9, 0, 0)

Human-intelligible date

>>> parse('Jan 9, 2019')
datetime.datetime(2019, 1, 9, 0, 0)
>>> parse('Jan 9, 2019 10:39 PM')
datetime.datetime(2019, 1, 9, 22, 39)

Hope you like this blog and don’t hesitate if you want to ask a queston or write a comment, it’s welcome!!

Reference