python datetime modular
Guide pack
from datetime import datetime
now = datetime.now()
print(now)
print(type(now))
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.microsecond)
from datetime import timedelta
Two datetime Object subtraction , What you get is a timedelta object
# Calculate the time interval
delta1 = datetime(2022, 6, 30, 20) - datetime(2022, 2, 2, 1)
print(delta1)
print(type(delta1))
Pictured , Express 148 Tianzero 19 Hours .
print(datetime(2022, 6, 10) + delta1)
timedelta Pass in a different number of parameters , The meaning of the expression is summarized as follows .
The first represents the number of days , The second represents the number of seconds , The third represents microseconds . The fourth represents milliseconds , The fifth represents minutes , The sixth represents the hour .
print(timedelta(10))
print(timedelta(10,11))
print(timedelta(10, 11, 12))
print(timedelta(10, 11, 12, 13))
print(timedelta(10, 11, 12, 13, 14))
print(timedelta(10, 11, 12, 13, 14, 15))
stamp = datetime(2022, 6, 22)
# Cast string
print(str(stamp))
# format transformation character string
print(stamp.strftime("%Y/%m/%d %H:%M:%S"))
print(stamp.strftime("%Y-%m-%d %H:%M:%S"))
print(stamp.strftime("%Y/%m/%d"))
print(stamp.strftime("%Y-%m-%d"))
dates = ['1/6/2022', '6/1/2022']
datelist = [datetime.strptime(i, "%m/%d/%Y") for i in dates]
print(datelist)
Parses a date in the form of a string into datetime object .
There are many ways to write strings , The following example .
from dateutil.parser import parse
print(parse('1/6/2022'))
print(parse('2022-6-2'))
print(parse('2022.6.3'))
print(parse('2022 6 4'))
print(parse('2022, 6, 5'))