📚 The CoCalc Library - books, templates and other resources
License: OTHER
Kernel: Python 2
This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Dates and Times
Basics
strftime
strptime
timedelta
Basics
In [1]:
In [2]:
In [3]:
In [4]:
Out[4]:
(7, 28, 15)
Extract the equivalent date object:
In [5]:
Out[5]:
datetime.date(2015, 1, 20)
Extract the equivalent time object:
In [6]:
Out[6]:
datetime.time(7, 28, 15)
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]:
Out[7]:
datetime.datetime(2015, 1, 20, 7, 0)
strftime
Format a datetime string:
In [8]:
Out[8]:
'01/20/2015 07:28'
strptime
Convert a string into a datetime object:
In [9]:
Out[9]:
datetime.datetime(2015, 1, 20, 0, 0)
timedelta
Get the current datetime:
In [10]:
Subtract two datetime fields to create a timedelta:
In [11]:
Out[11]:
datetime.timedelta(6, 40171, 885211)
Add a datetime and a timedelta to get a new datetime:
In [12]:
Out[12]:
datetime.datetime(2015, 1, 26, 18, 37, 46, 885211)