Python as a Pocket Calculater: the datetime Module

My favorite pocket-calculator use of Python is date math. The datetime library can be used to calculate the difference between two dates & times. For example:

>>> import datetime
>>>
print(datetime.datetime(2022,11,6,15,45,58) - datetime.datetime(2022,11,6,11,29,18))
4:16:40

Calculates the time elapsed between 11:29:18 AM and 3:45:58 PM today. Also:

>>> print(datetime.datetime(2022,11,6,15,45,58) + datetime.timedelta(minutes=496.885))
2022-11-07 00:02:51.100000

Tells what time it'll be when 496.885 minutes have elapsed since the earlier date.

6 November 2022

Previous
Previous

The Limits of Testing

Next
Next

PCRE Cheat Sheet