Date/Time Format¶
Please refer to https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes for a complete list of formats.
Time to String¶
In [1]:
import datetime
import pandas as pd
pd.options.display.max_colwidth = 100
now = datetime.datetime.now()
formats = [
("%Y-%m-%d", ""),
("%y-%m-%d", ""),
("%x", ""),
("%H:%M:%S", ""),
("%I:%M:%S", ""),
("%Y-%m-%d %H:%M:%S", ""),
("%Y-%m-%d %H:%M:%S.%f", ""),
("%Y%m%d_%H%M%S_%f", ""),
("%Y-%m-%d %X", ""),
("%Y-%m-%d %H:%M:%S %z", ""),
("%W", ""),
("%w", ""),
("%c", ""),
("%B", ""),
("%b", ""),
("%A", ""),
("%a", ""),
("%z", ""),
("%%", ""),
]
# examples = [[f, now.strftime(f)] for f in formats]
df = pd.DataFrame(formats, columns=["Fomat", "Comment"])
df.insert(1, column="Examples", value=[now.strftime(fmt) for fmt, _ in formats])
df
Out[1]:
Date to String¶
In [7]:
d = datetime.date(2017, 1, 9)
d
Out[7]:
In [8]:
d.strftime("%Y-%m-%d")
Out[8]:
In [9]:
str(d)
Out[9]:
In [ ]: