The datetime package
Contents
The datetime
package#
date objects#
from datetime import date
today_var = date.today()
today_var
datetime.date(2022, 11, 25)
today_var.year
2022
today_var.month
11
today_var.day
25
# monday = 0 & sunday = 6
today_var.weekday()
4
today_var.isoformat()
'2022-11-25'
birthday = date(year=1986, month=6, day=3)
birthday.isoformat()
'1986-06-03'
time objects#
from datetime import time
some_time = time(hour=16, minute=59, second=23, microsecond=254724)
some_time.isoformat()
'16:59:23.254724'
datetime objects#
from datetime import datetime
this_time = datetime.now()
this_time
datetime.datetime(2022, 11, 25, 12, 41, 32, 688888)
this_time.isoformat()
'2022-11-25T12:41:32.688888'
string versions of dates#
See the following table for date format codes. Also see here.
Directive |
Meaning |
Example |
---|---|---|
%a |
Weekday as locale’s abbreviated name. |
Sun, Mon |
%A |
Weekday as locale’s full name. |
Sunday, Monday |
%b |
Month as locale’s abbreviated name. |
Jan, Feb |
%B |
Month as locale’s full name. |
January, February |
%d |
Day of the month as a zero-padded decimal number. |
01, 02, …, 31 |
%m |
Month as a zero-padded decimal number. |
01, 02, …, 12 |
%y |
Year without century as a zero-padded decimal number. |
00, 01, …, 99 |
%Y |
Year with century as a decimal number. |
2013, 2014 |
%H |
Hour (24-hour clock) as a zero-padded decimal number. |
00, 01, …, 23 |
%I |
Hour (12-hour clock) as a zero-padded decimal number. |
01, 02, …, 12 |
%p |
Locale’s equivalent of either AM or PM. |
AM, PM |
%M |
Minute as a zero-padded decimal number. |
00, 01, …, 59 |
%S |
Second as a zero-padded decimal number. |
00, 01, …, 59 |
Print out a datetime value with a specific format. This will obviously work for dates or times as well.
d = datetime.now()
d.strftime('%I:%M %p on %B %d, %Y')
'12:41 PM on November 25, 2022'
dates from strings#
Suppose we have the following strings that need to be converted to dates/times/datetimes.
string_datetime = '10:48 PM on May 03, 2022'
string_date_1 = 'June 3, 1986'
string_date_2 = '6/3/86'
string_time = '11:15 PM'
We need to use the format codes in the previous table.
string_datetime = '10:48 PM on May 03, 2022'
datetime.strptime(string_datetime, '%I:%M %p on %B %d, %Y')
datetime.datetime(2022, 5, 3, 22, 48)
date
does not have attribute strptime()
. So if we technically want a date
and not a datetime
, then an additional step is required.
string_date_1 = 'June 3, 1986'
initial_obj = datetime.strptime(string_date_1, '%B %d, %Y')
date(
year=initial_obj.year,
month=initial_obj.month,
day=initial_obj.day,
)
datetime.date(1986, 6, 3)
string_date_2 = '6/3/86'
initial_obj = datetime.strptime(string_date_2, '%m/%d/%y')
date(
year=initial_obj.year,
month=initial_obj.month,
day=initial_obj.day,
)
datetime.date(1986, 6, 3)
time
does not have attribute strptime()
. So if we technically want a time
, then an additional step is required.
string_time = '11:15 PM'
initial_obj = datetime.strptime(string_time, '%I:%M %p')
time(
hour=initial_obj.hour,
minute=initial_obj.minute,
)
datetime.time(23, 15)
timedelta objects#
Attention
Need to add this section.