Python 3 - Datetime

Updated: 2018-12-09

Import datetime

>>> import datetime
>>> datetime
<module 'datetime' from '/path/to/datetime.py'>

Current Time

>>> datetime.datetime.now()
datetime.datetime(2020, 7, 22, 10, 39, 42, 204322)

Parse(String to Date, Time, or DateTime)

>>> datetime.datetime.strptime("12/31/2015", "%m/%d/%Y")
datetime.datetime(2015, 12, 31, 0, 0)
>>> datetime.datetime.strptime("12/31/2015", "%m/%d/%Y").date()
datetime.date(2015, 12, 31)
>>> datetime.datetime.strptime("12/31/2015", "%m/%d/%Y").time()
datetime.time(0, 0)

Print(Time to String)

>>> ts.strftime("%Y-%m-%d")
>>> datetime.date.today().strftime("%Y-%m-%d")
'2020-01-01'

Calculations

>>> d = datetime.datetime(2015, 12, 31)
>>> d
datetime.datetime(2015, 12, 31, 0, 0)

>>> d += datetime.timedelta(days=1)
>>> d
datetime.datetime(2016, 1, 1, 0, 0)

Absolute Time

Absolute time is the real-world time.

time.time() returns the seconds from 1970-01-01 00:00:00 UTC

>>> import time
>>> time.time()
1471119444.0186691

Relative Time

Relative time is based on a CPU counter, it has no relation with real world time.

time.perf_counter():

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

>>> time.perf_counter()
117611.291819553

time.process_time()

Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.

>>> time.process_time()
0.048191593000000005

Lookup Table

Directive Meaning Notes
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.