Python Dates and Times
When dealing with dates and times in Python, the ‘datetime’ module must be imported. Once imported, the current date and time can be assigned to a single variable, or separately to different variables.
date_time = datetime.datetime.now() date_only = datetime.datetime.now().date() time_only = datetime.datetime.now().time()
It is also possible to assign a specific date and time to a variable.
date_time = datetime.datetime(2017, 2, 1, 5, 31, 1, 1)
The values in the parenthesis refer to the year, month, day, hours, minutes, seconds and microsecond of the desired date and time, in this case 1 February 2017, along with a time of 5:31am.
If no formatting is applied, a ‘datetime’ variable can be displayed, for example, in the console, in a similar manner to other variables, such as strings, as shown below, using the ‘print’ function.
print(date_time)
This will display in the console as follows.
2017-02-01 05:31:01.000001
It should be noted that it is possible to omit the microsecond value when assigning a specific date and time to a variable.
date_time = datetime.datetime(2017, 2, 1, 5, 31, 1) print(date_time)
When this is output to the console the microsecond portion of the date and time is not present at all.
2017-02-01 05:31:01
If the seconds value is absent when the date and time is assigned to a variable, this is not omitted in the display, but instead, shown as two zeros.
2017-02-01 05:31:00
As well as being able to assign a specific date and time to a single variable, it is also possible to assign just the date or time.
date_only = datetime.date(2017, 2, 1) time_only = datetime.time(5, 31, 1, 1) print(date_only) print(time_only)
What gets displayed in the console here is the same as before, but split between the two variables.
2017-02-01 05:31:01.000001
Often, when the value of a ‘datetime’ variable is used, these formats aren’t ideal. Python allows for a number of different formats to be utilised.
print(date_time.strftime("%A, %d %B %Y"))
The above example displays the day of the week in words, represented by ‘%A’, followed by a comma, a two digit day, ‘%d’, the month in words, ‘%B’, and finally a four digit year, ‘%Y’.
Wednesday, 01 February 2017
Similarly, the format of the time portion can be specified.
print(date_time.strftime("%H:%M"))
Here, the time will be displayed with the hours, using a twenty four hour clock, represented by ‘%H’, followed by a colon and then two digits for the minutes, ‘%M’.
05:31
Below is a table showing format codes that can be used when displaying dates and times.
Format Code |
Description |
---|---|
%a | Three character abbreviation for the day of the week e.g. Mon for Monday. |
%A | Full name for the day of the week e.g. Monday. |
%w | Day of the week as a number from 0 through 6 e.g. 0 for Sunday. |
%d | Day of the month as a number from 01 through 31. |
%b | Three character abbreviation for the month e.g. Mar for March. |
%B | Full name for the month e.g. March. |
%m | Month as a number, with a leading zero e.g. 03 for March. |
%y | Year, without the century and a leading zero e.g. 09. |
%Y | Year, including the century e.g. 2017. |
%H | Hours, using a 24 hour clock, with a leading zero e.g. 20. |
%I | Hours, using a 12 hour clock, with a leading zero e.g. 05. |
%p | Time of day e.g. AM or PM. |
%M | Minutes, with a leading zero e.g. 06. |
%S | Seconds, with a leading zero e.g. 08. |
%f | Microseconds from 000000 through 999999. |
%z | UTC offset e.g. +0200. |
%Z | Time zone. |
%j | Three digit day of the year e.g. 001 for the first day of the year. |
%U | Week number of the year, with Sunday as the first day of the week, from 00 through 53. |
%W | Week number of the year, with Monday as the first day of the week, from 00 through 53. |
%c | Local version of the date and time e.g. Wed Feb 01 05:30:00 2017. |
%x | Local version of the date e.g. 02/01/17. |
%X | Local version of the time e.g. 05:30:00. |
%% | A literal percentage sign. |
As well as being able to format dates and times using the format codes above, Python also provides a number of properties to access individual components of a date and time, as shown below.
date_time = datetime.datetime(2017, 2, 1, 5, 31, 1, 1) print(date_time.year) print(date_time.month) print(date_time.day) print(date_time.hour) print(date_time.minute) print(date_time.second) print(date_time.microsecond)
The properties and methods discussed above are by no means exhaustive. There are many others available.