time 模块
time模块
timestamp 时间戳,1970年开始以秒计算
struct_tine时间元组
format_time格式化时间 (易读)
时间转换
'''
struct_time
/ \
/ \
/ \ localtime
strftime / strptime \ gttime
/ \
/ \
/ mktine \
/ \
/ \
format_time timestamp
(time.time)
%Y%m%D%H%M%S字符串
/ \
asctime / \ ctime
/ \
/ \
struct_time timestamp
'''
import time
print(time.time()) # 1562067574.5663896
print(time.mktime(time.localtime())) # 1562067651.0
print(time.localtime())
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=2, tm_hour=19, tm_min=41,
tm_sec=24, tm_wday=1, tm_yday=183, tm_isdst=0)
print(time.gmtime())
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=2, tm_hour=19, tm_min=41,
tm_sec=24, tm_wday=1, tm_yday=183, tm_isdst=0)
print(time.strptime('2017-2-2 16:16:16','%Y-%m-%d %H:%M:%S'))
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=2, tm_hour=16, tm_min=16,
tm_sec=16, tm_wday=3, tm_yday=33, tm_isdst=-1)
print(time.strftime('%Y-%m-%d %X')) # 2019-07-02 19:46:45
print(time.strftime('%Y-%m-%d %X',time.localtime())) #2019-07-02 19:47:25
星期
print(time.strftime('%a')) # Tue
print(time.strftime('%A')) # Tuesday
月份
print(time.strftime('%b')) # Jul
print(time.strftime('%B')) # July
时间日期都有
print(time.strftime('%c')) # Tue Jul 2 19:51:55 2019
一月的第几天
print(time.strftime('%d')) # 02
小时
print(time.strftime('%H')) # 19
一年的等几天
print(time.strftime('%j')) # 183
月份
print(time.strftime('%m')) # 07
分钟
print(time.strftime('%M')) # 54
AM或者PM
print(time.strftime('%p')) # PM
秒
print(time.strftime('%S')) # 14
第几个星期
print(time.strftime('%U')) # 26
一星期的第几天
print(time.strftime('%w')) # 2
星期一为一个星期的开始
print(time.strftime('%W')) # 26
相应的日期和时间
print(time.strftime('%x')) # 07/02/19
print(time.strftime('%X')) # 19:57:24
年
print(time.strftime('%y')) # 19
print(time.strftime('%Y')) # 2019
夏令时
print(time.strftime('%z')) # +0800
print(time.strftime('%%')) # %
print(time.strftime('%x %X')) # 07/02/19 20:06:24
t1 = time.time()
print(t1+10) # 1562069264.919341
datetime相当于对time模块进行了封装
import datetime
import time
d1 = datetime.date(2011,6,3)
print(d1.weekday()) # 4
print(d1.isoweekday()) # 5
print(d1.isocalendar()) # (2011, 22, 5)
print(datetime.datetime.now()) # 2019-07-02 20:22:54.840992
print(datetime.datetime.today()) # 2019-07-02 20:23:35.791064