How to use Date Time in python

# Import datetime module
import datetime
from datetime import timedelta, datetime

Today Date
print((datetime.now()).replace(hour=0, minute=0, second=0, microsecond=0)) print(datetime.today())
date = datetime.today()

print(date)
print(date.day)
print(date.year)
print(date.month)
print(date.hour)
print(date.minute)
print(date.second)
print(date.microsecond)

Create Static Date time object
datetime(2011, 07,07)

Python date object to Date str
print(datetime.today().strftime('%Y-%m-%d %H:%M:%S') )
print(datetime.today().strftime('%Y-%m-%d'))

Python date string to date object
print( datetime.strptime('20/02/2018','%d/%m/%Y') )
print(datetime.strptime('20/02/2018','%d/%m/%Y'))

Add Date and time in Date object
to_date = datetime.today() + timedelta(days=1, hours=5, minutes=30)
datetime.now().strftime('%Y%m%d%H%M%S')

change timespan to time
#timespan totsec = 14356
h = totsec//3600
m = (totsec%3600) // 60
sec =(totsec%3600)%60 #just for reference
print "%d:%d" %(h,m)

Comments

Popular posts from this blog

SQLAlchemy Create Table and Insert Data

SQLAlchemy ForeignKey Constraints