Python datetime use to judge the past seven days

table of Contents

strptime

Strptime used to format string

datetime.datetime.strptime("2019-10-02", "%Y-%m-%d")
# datetime.datetime(2019, 1, 2, 0, 10)

hour delta

Timedelta used to calculate the number of days

a = datetime.datetime.strptime("2019-10-02", "%Y-%m-%d")
b = datetime.datetime.strptime("2019-10-01", "%Y-%m-%d")
a - b
# datetime.timedelta(1)
(a - b).days
# 1

Guess you like

Origin www.cnblogs.com/daryl-blog/p/11003076.html