Change the defined character type date (eg 2020/7/13) to date type and add date type
test.py
import datetime
##(1)Define the date##
date0='2020/01/06' #Define date 0.
date1='2020/01/09' #Define date 1.
##(2)Convert from character type to date type##
tdate0=datetime.datetime.strptime(date0, '%Y/%m/%d') #Convert date 0 from character type to date type
tdate1=datetime.datetime.strptime(date1, '%Y/%m/%d') #Convert date 1 from character type to date type
print(type(tdate0)) #result:<class 'datetime.datetime'>
##Subtract date 1 and date 2##
dif=tdate1-tdate0 #Calculate the difference between date 1 and date 2.
print(dif.days) #Result: 3
##Add 2 days to date 1.
tdate2 = tdate1 + datetime.timedelta(days=2) #Add 2 days to date 1
print(tdate2) #Result: 2020-01-11 00:00:00
Recommended Posts