Converting string to datetime in Python
-
Converting a date in string format to a datetime object is required to perform date related operations like, add a number of days to a specified date, get a part of the date like year/month/days, compare 2 or more dates, sort dates, etc.
This can be achieved in python using a function named strptime().
At times, this process can get tricky because the string date can be in different formats and to convert this to a valid date, we will have to provide the correct format codes in the right order as per the string date.Here in this article, we have consolidated most of the common date format conversions for easier reference. Different date formats and the code formats required for conversion are listed below. Refer Convert Datetime to String - All Formats for converting datetime object to string in different formats.
If you have a list of dates that are in different formats, we can write a function to convert all these at once. Check Function to convert multiple string dates in different formats.
-
dd-mon-yy
from datetime import datetime stringdate = "28-OCT-90" formatteddate = datetime.strptime(stringdate, '%d-%b-%y') print(formatteddate)
1990-10-28 00:00:00 -
dd-mmm-yyyy
from datetime import datetime stringdate = "28-OCT-2010" formatteddate = datetime.strptime(stringdate, '%d-%b-%Y') print(formatteddate)
2010-10-28 00:00:00 -
dd-month-yy
from datetime import datetime stringdate = "28-OCTOBER-90" formatteddate = datetime.strptime(stringdate, '%d-%B-%y') print(formatteddate)
1990-10-28 00:00:00 -
dd-month-yyyy
from datetime import datetime stringdate = "28-OCTOBER-2015" formatteddate = datetime.strptime(stringdate, '%d-%B-%Y') print(formatteddate)
2015-10-28 00:00:00 -
mm/dd/yy
from datetime import datetime stringdate = "12/19/15" formatteddate = datetime.strptime(stringdate, '%m/%d/%y') print(formatteddate)
2015-12-19 00:00:00 -
mm/dd/yyyy
from datetime import datetime stringdate = "12/19/2022" formatteddate = datetime.strptime(stringdate, '%m/%d/%Y') print(formatteddate)
2022-12-19 00:00:00 -
dd.mm.yy
from datetime import datetime stringdate = "20.05.22" formatteddate = datetime.strptime(stringdate, '%d.%m.%y') print(formatteddate)
2022-05-20 00:00:00 -
dd.mm.yyyy
from datetime import datetime stringdate = "20.05.2021" formatteddate = datetime.strptime(stringdate, '%d.%m.%Y') print(formatteddate)
2021-05-20 00:00:00 -
yy/mm/dd
from datetime import datetime stringdate = "20/04/25" formatteddate = datetime.strptime(stringdate, '%y/%m/%d') print(formatteddate)
2020-04-25 00:00:00 -
yyyy/mm/dd
from datetime import datetime stringdate = "2020/04/25" formatteddate = datetime.strptime(stringdate, '%Y/%m/%d') print(formatteddate)
2020-04-25 00:00:00 -
hh:mm (24 Hour)
from datetime import datetime stringdate = "15:35" formatteddate = datetime.strptime(stringdate, '%H:%M') print(formatteddate)
1900-01-01 15:35:00 -
hh:mm pm
from datetime import datetime stringdate = "11:35 PM" formatteddate = datetime.strptime(stringdate, '%I:%M %p') print(formatteddate)
1900-01-01 23:35:00 -
hh:mm:ss.ms
from datetime import datetime stringdate = "22:35:55.123456" formatteddate = datetime.strptime(stringdate, '%H:%M:%S.%f') print(formatteddate)
1900-01-01 22:35:55.123456 -
mm:ss
from datetime import datetime stringdate = "20:35" formatteddate = datetime.strptime(stringdate, '%M:%S') print(formatteddate)
1900-01-01 00:20:35 -
dd-mmm-yyyy hh:mm
from datetime import datetime stringdate = "28-OCT-2010 20:35" formatteddate = datetime.strptime(stringdate, '%d-%b-%Y %H:%M') print(formatteddate)
2010-10-28 20:35:00 -
dd-mmm-yyyy hh:mm:ss.ms
from datetime import datetime stringdate = "28-OCT-2010 20:35:55.234567" formatteddate = datetime.strptime(stringdate, '%d-%b-%Y %H:%M:%S.%f') print(formatteddate)
2010-10-28 20:35:55.234567 -
dd-month-yyyy hh:mm
from datetime import datetime stringdate = "28-NOVEMBER-2010 20:35" formatteddate = datetime.strptime(stringdate, '%d-%B-%Y %H:%M') print(formatteddate)
2010-11-28 20:35:00 -
dd-month-yyyy hh:mm:ss.ms
from datetime import datetime stringdate = "28-NOVEMBER-2010 20:35:55.234567" formatteddate = datetime.strptime(stringdate, '%d-%B-%Y %H:%M:%S.%f') print(formatteddate)
2010-11-28 20:35:55.234567 -
yyyy-mm-dd hh:mm
from datetime import datetime stringdate = "2022-01-25 20:35" formatteddate = datetime.strptime(stringdate, '%Y-%m-%d %H:%M') print(formatteddate)
2022-01-25 20:35:00 -
yyyy-mm-dd hh:mm:ss.ms
from datetime import datetime stringdate = "2022-01-25 20:35:10.34567" formatteddate = datetime.strptime(stringdate, '%Y-%m-%d %H:%M:%S.%f') print(formatteddate)
2022-01-25 20:35:10.345670
-
Format Codes
Below are the list of the different Format Codes used for conversion.
Code Description Sample %a Weekday (abbr.) as per locale Sun, Mon (for en US) %A Weekday full as per locale Sunday, Monday (for en US) %b Month (abbr.) as per locale Jan, Feb (for en US) %B Month full as per locale January, February (for en US) %c Date and time as per locale Wed Jan 15 20:45:00 2022 (for en US) %d Day of the month 00, 01, ..., 31 %f Micro seconds 000000 to 999999 %H Hour in 24 hour format 00 to 23 %I Hour in 12 hour format 01 to 12 %j Day of the year 001 to 366 %m Month as number 01 to 12 %M Minute 00 to 59 %p AM or PM as per locale AM, PM (for en US) %S Seconds 00 to 59 %U Week number of the year. Sunday as first day of week 00 to 53 %w Week day number with Sunday as first day (0) 0 to 6 %W Week number of the year. Monday as first day of week 00 to 53 %x Date as per locale 01/15/2022 (for en US) %X Time as per locale 20:45:00 (for en US) %y 2 digit year 00 to 99 %Y 4 digit year 0001 to 9999 %z UTC offset as +-HHMM[SS[.ffffff]] +0124, -041025, +010203. 555555 %Z Time zone UTC, GMT, etc