py datetime code example

Example 1: string to date python

import datetime

date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')

print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)

Example 2: datetime python

from datetime import datetime as d
date = d.now()
print(date.strftime("%Y-%m-%d %H:%M:%S"))

Example 3: strftime python

date_time = now.strftime("%m/%d/%Y, %H:%M:%S")

Example 4: python timedelta

from datetime import timedelta 
tomorrow = datetime.today() + timedelta(days = 1)

Example 5: datetime python

>>> timedelta(hours=-5)
datetime.timedelta(days=-1, seconds=68400)
>>> print(_)
-1 day, 19:00:00