2.7. Datetime#

2.7.1. datetime + timedelta: Calculate End DateTime Based on Start DateTime and Duration#

Provided an event starts at a certain time and takes a certain number of minutes to finish, how do you determine when it ends?

Taking the sum of datetime and timedelta(minutes) will do the trick.

from datetime import date, datetime, timedelta

beginning = '2020/01/03 23:59:00'
duration_in_minutes = 2500

# Find the beginning time
beginning = datetime.strptime(beginning, '%Y/%m/%d %H:%M:%S')

# Find duration in days
days = timedelta(minutes=duration_in_minutes)

# Find end time
end = beginning + days 
end
datetime.datetime(2020, 1, 5, 17, 39)

2.7.2. Use Dates in a Month as the Feature#

Have you ever wanted to use dates in a month as the feature in your time series data? You can find the days in a month by using calendar.monthrange(year, month)[1] like below.

import calendar 

calendar.monthrange(2020, 11)[1]
30

2.7.3. Use Comparison and Arithmetic Operators on Dates in Python#

In Python, you can compare and subtract dates using operators.

from datetime import date 

date1 = date(2022, 1, 1)
date2 = date(2022, 11, 1)

if date1 < date2:
    diff = date2 - date1 
else:
    diff = date1 - date2 

print(f"{date1} and {date2} is {diff} apart.")
2022-01-01 and 2022-11-01 is 304 days, 0:00:00 apart.