01x01
1/31/2019 - 12:34 PM

datatime 模块

# 时间对象 转字符串
from datetime import datetime 
datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S")
# 字符串对象转时间对象
from datetime import datetime 
>>> datetime.strptime('2018-09-09',"%Y-%m-%d")
datetime.datetime(2018, 9, 9, 0, 0)
# GMT 时间格式
GMT_FORMAT =  '%a, %d %b %Y %H:%M:%S GMT'
# 美国时间 和 UTC 时间 转换
datetime.strftime(datetime.utcnow()-timedelta(hours=8),'%Y-%m-%d %H:%M:%S')
SymbolMeaningExample
%aAbbreviated weekday name'Wed'
%AFull weekday name'Wednesday'
%wWeekday number: 0 (Sunday) through 6 (Saturday)'3'
%dDay of the month (zero padded)'13'
%bAbbreviated month name'Jan'
%BFull month name'January'
%mMonth of the year'01'
%yYear without century'18'
%YYear with century'2018'
%HHour from 24-hour clock'17'
%IHour from 12-hour clock'05'
%pAM/PM'PM'
%MMinutes'00'
%SSeconds'00'
%fMicroseconds'000000'
%zUTC offset for time zone–aware objects'-0500'
%ZTime zone name'EST'
%jDay of the year'013'
%WWeek of the year'02'
%cDate and time representation for the current locale'Wed Jan 13 17:00:00 2016'
%xDate representation for the current locale'01/13/16'
%XTime representation for the current locale'17:00:00'
%%A literal % character'%'