7/08/2021

python date time add or subtract

datetime package is very useful.

see the example code..

..
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now().date())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(days = +10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(hours = 10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
lastHourDateTime = datetime.datetime.now() + datetime.timedelta(hours = -10)
print(lastHourDateTime.date())
print(lastHourDateTime.time())
..
result
2021-07-08 10:19:44.969082
2021-07-08
2021-07-18
10:19:44.969146
2021-07-08
20:19:44.969164

Thank you.

study.marearts.com


No comments:

Post a Comment