This notebook was prepared by Donne Martin. Source and license info is on GitHub.
In [1]:
from datetime import datetime, date, time
In [2]:
year = 2015
month = 1
day = 20
hour = 7
minute = 28
second = 15
In [3]:
dt = datetime(year, month, day, hour, minute, second)
In [4]:
dt.hour, dt.minute, dt.second
Out[4]:
Extract the equivalent date object:
In [5]:
dt.date()
Out[5]:
Extract the equivalent time object:
In [6]:
dt.time()
Out[6]:
When aggregating or grouping time series data, it is sometimes useful to replace fields of a series of datetimes such as zeroing out the minute and second fields:
In [7]:
dt.replace(minute=0, second=0)
Out[7]:
Format a datetime string:
In [8]:
dt.strftime('%m/%d/%Y %H:%M')
Out[8]:
Convert a string into a datetime object:
In [9]:
datetime.strptime('20150120', '%Y%m%d')
Out[9]:
Get the current datetime:
In [10]:
dt_now = datetime.now()
Subtract two datetime fields to create a timedelta:
In [11]:
delta = dt_now - dt
delta
Out[11]:
Add a datetime and a timedelta to get a new datetime:
In [12]:
dt + delta
Out[12]: