Use the datetime module to convert different time units
Python code
from datetime import timedelta a = timedelta(days=2, hours=6) b = timedelta(hours=4.5) c = a + b print(c.days) print(c.seconds) print(c.seconds / 3600)
Represents a specific date and time
Java code
from datetime import datetime a = datetime(2016, 8, 30) print(a + timedelta(days=10)) b = datetime(2016, 9, 30) d = b - a print(d.days) now = datetime.today() print(now) print(now + timedelta(minutes=10))
The datetime module can correctly handle leap years
Java code
a = datetime(2012, 3, 1) b = datetime(2012, 2, 28) print((a-b).days) c = datetime(2013, 3, 1) d = datetime(2013, 2, 28) print((c-d).days)
Processing is more complicated For date issues, such as dealing with time zones, fuzzy time ranges, calculating holiday dates, etc., you can use the dateutil module