Because dates contain multiple pieces of different information, it makes the most sense to store them within their own module, so that it is easier to easily manipulate and extract information from them.
One simple command we can use is the .now() function to see the current timestamp of when a program is being run:
import datetime
current_timestamp = datetime.datetime.now()
print(current_timestamp)
# Prints a formatted timestamp like: 2023-07-28 16:58:29.409742
The timestamp that gets printed out should be relatively easy to decipher. It comes in the format: year-month-day hours:minutes:seconds.milliseconds.
This is the default format we will encounter, but it is possible and more practical to extract individual parts of a date using the following directives:
Directive |
Meaning |
Example |
---|---|---|
|
Weekday as locale’s abbreviated name. |
Sun, Mon, …, Sat
(en_US);
So, Mo, …, Sa
(de_DE)
|
|
Weekday as locale’s full name. |
Sunday, Monday, …,
Saturday (en_US);
Sonntag, Montag, …,
Samstag (de_DE)
|
|
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. |
0, 1, …, 6 |
|
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
|
Month as locale’s abbreviated name. |
Jan, Feb, …, Dec
(en_US);
Jan, Feb, …, Dez
(de_DE)
|
|
Month as locale’s full name. |
January, February,
…, December (en_US);
Januar, Februar, …,
Dezember (de_DE)
|
|
Month as a zero-padded decimal number. |
01, 02, …, 12 |
|
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
|
Year with century as a decimal number. |
0001, 0002, …, 2013, 2014, …, 9998, 9999 |
|
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
|
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
|
Locale’s equivalent of either AM or PM. |
AM, PM (en_US);
am, pm (de_DE)
|
|
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
|
Second as a zero-padded decimal number. |
00, 01, …, 59 |
|
Microsecond as a decimal number, zero-padded to 6 digits. |
000000, 000001, …, 999999 |
|
UTC offset in the form
|
(empty), +0000, -0400, +1030, +063415, -030712.345216 |
|
Time zone name (empty string if the object is naive). |
(empty), UTC, GMT |
|
Day of the year as a zero-padded decimal number. |
001, 002, …, 366 |
|
Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. |
00, 01, …, 53 |
|
Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0. |
00, 01, …, 53 |
|
Locale’s appropriate date and time representation. |
Tue Aug 16 21:30:00
1988 (en_US);
Di 16 Aug 21:30:00
1988 (de_DE)
|
|
Locale’s appropriate date representation. |
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
|
|
Locale’s appropriate time representation. |
21:30:00 (en_US);
21:30:00 (de_DE)
|
|
A literal |
% |
Using the above directives, I could extract very easily individual parts of the timestamp using the special strftime() function:
import datetime
current_timestamp = datetime.datetime.now()
# Print the day of the week:
print(current_timestamp.strftime("%A")) # prints "Friday"
# Print the current month:
print(current_timestamp.strftime("%B")) # prints "July"
# Print the current time in a 12-hour format:
print(current_timestamp.strftime("%I:%M %p")) # Prints "05:08 PM"
As you can see, there are many different parts of the date that may be beneficial to us, and the strftime() function helps us extract these parts. Feel free to play around more with these on your own.
To create a date in the past or future, we can use the datetime() class of the datetime module.
The datetime() class requires three parameters to create a date: year, month, day.
import datetime
new_date = datetime.datetime(2016, 11, 2)
print(new_date) # Prints "2016-11-02 00:00:00"
If we want to create a datetime object from a different format besides just the year, month, and day, we can also use the strptime() function. This takes in two parameters: a string containing the date and/or time, and a format containing the directives in the table of detailing how to decipher the date string. For instance:
from datetime import datetime
datetime_str = '09/19/18 13:55:26'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
print(datetime_object) # Prints "2018-09-19 13:55:26"
Notice how the second parameter passed through the strptime() function includes the appropriate directives in order to decipher the passed in the first parameter.