Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

import datetime
import pandas as pd

pd.options.display.max_colwidth = 100
now = datetime.datetime.now()
formats = [
    ("%Y-%m-%d", ""),
    ("%y-%m-%d", ""),
    ("%B %d, %Y", ""),
    ("%b %d, %Y", ""),
    ("%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
Loading...

Date to String

d = datetime.date(2017, 1, 9)
d
datetime.date(2017, 1, 9)
d.strftime("%Y-%m-%d")
'2017-01-09'
str(d)
'2017-01-09'