and the string conversion between datetime python

A, datetime generation

from datetime import datetime
# 当时时间
now=datetime.now()
# 指定时间
test=datetime(2020,1,26,11,11,11)

Two, datetime to String

  • str strong turn type
str(datetime(2020,1,3))
  • strftime: based on the incoming format
datetime.now().strftime('%Y-%m-%d')

Third, the switch string datetime

  • strptime: parsing format known time
datetime.strptime('1/26/2021','%m/%d/%Y')
  • dateutil.parse can resolve almost all human beings can understand date representation
from dateutil.parser import parse
parse('2020-01-03')
parse('Jan 31, 2021 10:45 PM')

Four, datetime time difference calculating

timedelta: represents the difference between the two datetime (date, seconds, milliseconds)

# datetime相减
datetime(2019,1,7) - datetime(2021,6,24,8,15)
# datetime加timedelta
datetime(2021,1,7)+timedelta(12)

Guess you like

Origin www.cnblogs.com/chenqionghe/p/12235277.html