import time #1553580512.129654 时间戳格式 #时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。 print(time.time()) #time.struct_time(tm_year=2019, tm_mon=3, tm_mday=26, tm_hour=14, tm_min=9, tm_sec=3, tm_wday=1, tm_yday=85, tm_isdst=0) 元祖格式 print(time.localtime()) #可以把时间戳格式的值传入元祖格式最中,进行转换 #time.struct_time(tm_year=2016, tm_mon=1, tm_mday=24, tm_hour=4, tm_min=21, tm_sec=52, tm_wday=6, tm_yday=24, tm_isdst=0) print(time.localtime(1453580512.129654)) #2019 对元祖时间格式,我们可以做操作,取里面的特定年月日 time_tuple = time.localtime() print(time_tuple.tm_year) # 2019-03-26 14:11:04 字符串格式 print( time.strftime("%Y-%m-%d %X")) #时间格式的转换 #1.时间戳格式 -----> 元祖格式转换 #localtime/gmtime print(time.localtime(3600*24)) print(time.gmtime(3600*24)) #2.元祖格式 -----> 时间戳格式转换 #mktime print(time.mktime(time.localtime())) #3.元祖格式 -----> 字符串格式转换 print(time.strftime("%Y-%m-%d %X", time.localtime())) #4.字符串格式 -----> y元祖格式转换 print(time.strptime("2019-03-26","%Y-%m-%d")) #5.time sleep 功能 #睡3秒
time.sleep(3)
时间格式转换图:
format time结构化表示
格式 | 含义 |
%a | 本地(locale)简化星期名称 |
%A | 本地完整星期名称 |
%b | 本地简化月份名称 |
%B | 本地完整月份名称 |
%c | 本地相应的日期和时间表示 |
%d | 一个月中的第几天(01 - 31) |
%H | 一天中的第几个小时(24小时制,00 - 23) |
%I | 第几个小时(12小时制,01 - 12) |
%j | 一年中的第几天(001 - 366) |
%m | 月份(01 - 12) |
%M | 分钟数(00 - 59) |
%p | 本地am或者pm的相应符 |
%S | 秒(01 - 61) |
%U | 一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。 |
%w | 一个星期中的第几天(0 - 6,0是星期天) |
%W | 和%U基本相同,不同的是%W以星期一为一个星期的开始。 |
%x | 本地相应日期 |
%X | 本地相应时间 |
%y | 去掉世纪的年份(00 - 99) |
%Y | 完整的年份 |
%Z | 时区的名字(如果不存在为空字符) |
%% | ‘%’字符 |
DATE类:
from datetime import * import time # date.max: 9999-12-31 # date.min: 0001-01-01 # date.today(): 2019-03-26 # date.fromtimestamp(): 2019-03-26 print('date.max:', date.max) print('date.min:', date.min) print('date.today():', date.today()) print('date.fromtimestamp():', date.fromtimestamp(time.time())) # now: 2019-03-26 , tomorrow: 2019-03-27 # timetuple(): time.struct_time(tm_year=2019, tm_mon=3, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=85, tm_isdst=-1) # weekday(): 1 # isoweekday(): 2 # isocalendar(): (2019, 13, 2) # isoformat(): 2019-03-26 # ('strftime():', '2019-03-26') now = date(2019, 3, 26) tomorrow = now.replace(day = 27) print('now:', now, ', tomorrow:', tomorrow) print('timetuple():', now.timetuple()) print('weekday():', now.weekday()) print('isoweekday():', now.isoweekday()) print('isocalendar():', now.isocalendar()) print('isoformat():', now.isoformat()) print(('strftime():', now.strftime("%Y-%m-%d")))
DATETIME类:
from datetime import * import time print('############################################') # datetime.max: 9999-12-31 23:59:59.999999 # datetime.min: 0001-01-01 00:00:00 # datetime.resolution: 0:00:00.000001 # today(): 2019-03-26 14:31:24.194149 # now(): 2019-03-26 14:31:24.194149 # utcnow(): 2019-03-26 06:31:24.194149 # fromtimestamp(tmstmp): 2019-03-26 14:31:24.194149 # ('utcfromtimestamp(tmstmp):', datetime.datetime(2019, 3, 26, 6, 31, 24, 194149)) print('datetime.max:', datetime.max) print('datetime.min:', datetime.min) print('datetime.resolution:', datetime.resolution) print('today():', datetime.today()) print('now():', datetime.now()) print('utcnow():', datetime.utcnow()) print('fromtimestamp(tmstmp):', datetime.fromtimestamp(time.time())) print(('utcfromtimestamp(tmstmp):', datetime.utcfromtimestamp(time.time()))) # 方法和属性 # dt=datetime.now()#datetime对象 # dt.year、month、day、hour、minute、second、microsecond、tzinfo: # dt.date():获取date对象; # dt.time():获取time对象; # dt. replace ([ year[ , month[ , day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] ] ] ]): # dt. timetuple () # dt. utctimetuple () # dt. toordinal () # dt. weekday () # dt. isocalendar () # dt. isoformat ([ sep] ) # dt. ctime ():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple())); # dt. strftime (format) # import time