Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

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


1/27/2020

python year month day expression using datetime

Without any separator
import datetime
datetime.datetime.now().strftime("%Y%m%d%H%M%S")
Using - as separator
import datetime
datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
Using _ as separator
import datetime
datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")