Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Hands on the datetime Module in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Comment

A Datetime object cannot be compared with a string directly. However, pandas Series of datetime objects can be compared with string date directly (both by operators and methods). The comparison is done by parsing the string to a datetime object. An exception will be thrown if the string cannot be parsed into a datetime object.

import datetime

Construct a date/datetime Object from String

datetime.datetime.strptime("2017-01-09", "%Y-%m-%d")
datetime.datetime(2017, 1, 9, 0, 0)

Time in micro seconds resolution.

datetime.datetime.strptime("2017-01-18 09:21:29.000000", "%Y-%m-%d %H:%M:%S.%f")
datetime.datetime(2017, 1, 18, 9, 21, 29)

Use suffixing zeros if nano seconds is provided.

datetime.datetime.strptime("2017-01-18 09:21:29.000000000", "%Y-%m-%d %H:%M:%S.%f000")
datetime.datetime(2017, 1, 18, 9, 21, 29)

Construct a date/datetime Object by Specifying Year, Month, Day, etc.

datetime.date(2017, 1, 9)
datetime.date(2017, 1, 9)

Construct a date/datetime Object from Timestamp

datetime.fromtimestamp(t3)

now/today

Get the current system time.

datetime.now()
datetime.datetime(2018, 5, 6, 4, 24, 0, 949796)
str(datetime.now())
'2018-05-06 10:11:57.277133'
datetime.today()
datetime.datetime(2018, 5, 6, 4, 23, 58, 870716)

Get timezone name. By default, datetime is not aware of timezone.

Get the current UTC time.

utcnow

datetime.utcnow()
datetime.datetime(2018, 5, 6, 4, 9, 42, 210413)
datetime.utcnow().date()
datetime.date(2018, 5, 6)

It seems that now and today are equivalent.

date

datetime.now().date()
datetime.date(2018, 5, 6)

Timzezone

By default, datetime is not aware of timezone which is fucking stupid! For this reason, the utcnow does return UTC time at all!

datetime.today().tzname()
datetime.now().tzname()

now and utcnow are the same, which is fucking stpid and error-prone!

print(datetime.now())
print(datetime.utcnow())
2018-05-06 04:25:45.744147
2018-05-06 04:25:45.744282

Timestamp

datetime.now().timestamp()
1525600136.106702
datetime.utcnow().timestamp()
1525600384.425062
datetime.fromtimestamp(datetime.now().timestamp())
datetime.datetime(2018, 5, 6, 10, 11, 1, 920892)

Month Day

date.today().day
11

Weekday

Monday is 0 and Sunday is 6.

today = date.today()
today
datetime.date(2017, 10, 11)
date.today().weekday() - 5 + 7
4
today - 4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-ac1ac82a5046> in <module>()
----> 1 today - 4

TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'

TimeDelta

Arithmatical operation is supported via the TimeDelta class.

https://stackoverflow.com/questions/441147/how-can-i-subtract-a-day-from-a-python-date

from datetime import date, timedelta

d = date.today() - timedelta(days=1)
d
datetime.date(2018, 6, 28)
timedelta(days=1).days
1

Convert a datetime Object to String

By default, a datetime object is convertd to a string using the format %Y-%m-%d %H:%M:%S.%f. Please refer to Format Date and Time in Python for more details on how format a datetime object.

now = datetime.datetime.now()
now
datetime.datetime(2020, 3, 11, 11, 53, 42, 533996)
str(now)
'2020-03-11 11:53:42.533996'
f"The time now is {now}"
'The time now is 2020-03-11 11:53:42.533996'