Python doesn't have the
date as built-in data type. However, the
datetime module in the standard library defines
datetime classes using which date and time related processing can be done.
Objects of these classes are created by providing integer arguments as following:
dateobj = date(year, month, day) timeobj = time(hour, minute, second) datetimeobj = datetime(year, month, day, hour, minute, second)
Hence, we should be able to parse string representation of date/time and obtain arguments for the above constructors. For example, the default string representation of the date is
'YYYY-MM-DD', for time
'YYYY-MM-DD HH:MM:SS' is for the datetime.
'2020-10-16 12:35:20' is a string representing date and time. We can manually separate date and time attributes and construct the
datetime object as follows:
>>> strdate="2020-10-16 12:35:20" >>> dt_tuple=tuple([int(x) for x in strdate[:10].split('-')])+tuple([int(x) for x in strdate[11:].split(':')]) >>> dt_tuple (2020, 10, 16, 12, 35, 20)
In the above datetime string, the first 10 characters have a year, month and day values separated by '-' and the remaining 8 characters have an hour, min and sec values separated by ':'. You can now unpack the above tuple for the datetime constructor by importing the datetime module, as shown below.
>>> import datetime >>> datetimeobj=datetime.datetime(*dt_tuple) >>> datetimeobj datetime.datetime(2020, 10, 16, 12, 35, 20)
However, the string representation of date and time may vary as per the user's choice. For example, one may use
DD-MM-YY format, or someone may want to show month by name, such as
This is where the
strptime() method of the
datetime class proves useful.
The first parameter is the string representation, and the second uses predefined codes to parse date and time values from a string. For example, a date string
'2020-10-16' is parsed by this function if the format given is
%Y uses the first four characters as four digit representation of a year, the middle two characters as a month and the last two for a date.
The module defines the following unique codes for formatting datetime string according to their predefined meaning.
|%a||Weekday, short version|
|%A||Weekday, full version|
|%d||Day of month 01-31|
|%b||Month name, short version|
|%B||Month name, full version|
|%m||Month as a number 01-12|
|%y||Year, short version, without century|
|%Y||Year, full version|
Let's use the
strptime() method to convert a given string to a datetime object, as shown below:
>>> strdate="2020-10-16 12:35:20" >>> datetimeobj=datetime.datetime.strptime(strdate, "%Y-%m-%d %H:%M:%S") >>> datetimeobj datetime.datetime(2020, 10, 16, 12, 35, 20)
If the date string is changed to
'DD-MM-YY', the format has to be set to
>>> strdate="16-10-2020 12:35:20" >>> datetimeobj=datetime.datetime.strptime(strdate,"%d-%m-%Y %H:%M:%S") >>> datetimeobj datetime.datetime(2020, 10, 16, 12, 35, 20)
As a final example, let us set date string as
>>> strdate="16-Oct-20" >>> datetimeobj=datetime.datetime.strptime(strdate,"%d-%b-%y") >>> datetimeobj datetime.datetime(2020, 10, 16, 0, 0)
Note that time parameters appear as 0 as they are not included in the string.