1 time.timezone #以秒为单位显示时区
>>> import time >>> time.timezone -28800
北京为东八区,所以为-28800
2 time.altzone #以秒为单位返回与UTC的时间差
>>> time.altzone -32400
3 time.daylight #返回夏时制,0代表不是夏时制,1代表是夏时制
>>> time.daylight
0
4 time.time() # 返回时间戳
>>> time.time()
1571626440.597493
5 time.sleep() #等待几秒
>>> time.sleep(3)
>>>
6 time.getime() #返回UTC struct_time,结果是个元祖
>>> time.gmtime() time.struct_time(tm_year=2019, tm_mon=10, tm_mday=21, tm_hour=3, tm_min=1, tm_sec=11, tm_wday=0, t m_yday=294, tm_isdst=0)
>>> time.gmtime(1000000000)
time.struct_time(tm_year=2001, tm_mon=9, tm_mday=9, tm_hour=1, tm_min=46, tm_sec=40, tm_wday=6, tm_yday=252, tm_isd
st=0)
7 time.localtime() #返回当地时间 struct_time
>>> time.localtime()#不加参数 默认返回当前时间 time.struct_time(tm_year=2019, tm_mon=10, tm_mday=21, tm_hour=11, tm_min=2, tm_sec=46, tm_wday=0, tm_yday=294, tm_isdst=0)
>>> time.localtime(1000000000)#添加参数,根据参数计算
time.struct_time(tm_year=2001, tm_mon=9, tm_mday=9, tm_hour=9, tm_min=46, tm_sec=40, tm_wday=6, tm
_yday=252, tm_isdst=0)
8 获取struct_time数据
>>> x = time.localtime() >>> x.tm_year 2019
9 struct_time转换为时间戳
>>> x = time.localtime() >>> x time.struct_time(tm_year=2019, tm_mon=10, tm_mday=21, tm_hour=11, tm_min=12, tm_sec=54, tm_wday=0, tm_yday=294, tm_ isdst=0) >>> y = time.mktime(x) >>> y 1571627574.0
10 格式化时间 struct_time --->格式化时间
>>> x = time.localtime() >>> x time.struct_time(tm_year=2019, tm_mon=10, tm_mday=21, tm_hour=11, tm_min=16, tm_sec=31, tm_wday=0, tm_yday=294, tm_ isdst=0) >>> z = time.strftime('%Y-%m-%d %H:%M:%S',x)#先写格式 后传struct_time >>> z '2019-10-21 11:16:31' >>>
格式化编码说明
Commonly used format codes: %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.
11 格式化时间 格式化时间 --->stuct_time
>>> a = time.strptime('2360-02-29 22:45:12','%Y-%m-%d %H:%M:%S')#先写格式化时间,在写格式 >>> a time.struct_time(tm_year=2360, tm_mon=2, tm_mday=29, tm_hour=22, tm_min=45, tm_sec=12, tm_wday=0, tm_yday=60, tm_is dst=-1) >>>
12 便捷获取格式化时间
>>> b = time.asctime()#空参数默认返回当前时间 >>> b 'Mon Oct 21 11:22:13 2019'#默认格式就是这样 星期 月 日 小时 分钟 秒
>>> c = time.ctime()#同asctime()
>>> c
'Mon Oct 21 11:25:00 2019'
13 时间加减
>>> import datetime >>> datetime.datetime.now() datetime.datetime(2019, 10, 21, 11, 27, 2, 236054) >>> datetime.datetime.now()+datetime.timedelta(3) datetime.datetime(2019, 10, 24, 11, 29, 26, 657239) >>> datetime.datetime.now()+datetime.timedelta(-3) datetime.datetime(2019, 10, 18, 11, 30, 6, 553413) >>> datetime.datetime.now()+datetime.timedelta(hours=3)