Import module


In [28]:
from datetime import datetime
import time
from matplotlib.dates import date2num

Datetime -> String


In [39]:
datetime_object = datetime(2015, 8, 7)
date_str = datatime_object.strftime("%Y-%m-%d") # %Y-%m-%d %H:%M:%S
date_str # string type


Out[39]:
'2015-08-07'

In [41]:
datetime(2015, 8, 7).strftime("%Y-%m-%d") # datetime(2015, 8, 7, 20,6,39)


Out[41]:
'2015-08-07'

String -> Datetime


In [49]:
string = "2015-08-05"
datetime.strptime(string, "%Y-%m-%d")


Out[49]:
datetime.datetime(2015, 8, 5, 0, 0)

In [46]:
string = "2015-08-05 17:50:30"
datetime.strptime(string, "%Y-%m-%d %H:%M:%S")


Out[46]:
datetime.datetime(2015, 8, 5, 17, 50, 30)

Datetime/Time to number


In [50]:
date2num(datetime.strptime("2015-08-05","%Y-%m-%d"))


Out[50]:
735815.0

In [51]:
time.mktime(time.strptime("2015-08-05","%Y-%m-%d"))


Out[51]:
1438700400.0