Python time difference calculation

Time:2021-8-4

Method 1: the time to be calculated is a fixed value

import datetime
c="2020-12-08 11:30:00"
d1=datetime.datetime.strptime(str(c),"%Y-%m-%d %H:%M:%S")
d3 = d1 - datetime.timedelta(seconds=900)
d3.ctime()

Method 2: use the current time for calculation

Datetime can view: days, hours, weekday (); Timedelta can view: days, seconds, etc.

import datetime
d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(hours=10)
d3.ctime()