Cheat Sheet on dates

Links: notebook, html, PDF, python, slides, slides(2), GitHub

Cheat sheet on dates.

from jyquickhelper import add_notebook_menu
add_notebook_menu()

string to datetime and datetime to string

see how to convert a string date into datetime format in python

import datetime
d = datetime.datetime.strptime("11/08/75", "%d/%m/%y")
d
datetime.datetime(1975, 8, 11, 0, 0)
d.strftime("%Y-%m-%d")
'1975-08-11'

now, time difference

from datetime import datetime
now = datetime.now()
now
datetime.datetime(2017, 11, 9, 11, 30, 20, 895311)
delta = now - datetime(now.year - 1, 12, 31)
delta
datetime.timedelta(313, 41452, 134562)
delta.days
313

day of the year

now = datetime.now()
now
datetime.datetime(2017, 11, 9, 11, 31, 7, 946012)
day_of_year = now.timetuple().tm_yday
day_of_year
313