时间模块
time模块
时间表示格式:
1.格式化时间: 【 2018-01-01 01:00:00 】
2.时间戳: 【 1518407077.940927 】 -> 秒数(从1970开始到现今)
3.以元组方式表示,九个元素(格式化元组)
元素如下:
(1) tm_year=2018, # 年
(2) tm_mon=2, # 月
(3) tm_mday=12, # 日
(4) tm_hour=11, # 小时
(5) tm_min=49, # 分钟
(6) tm_sec=13, # 秒
(7) tm_wday=0, # 0(Mon)-6(Sun) 从0(周一)开始数 -> 6(周天)
(8) tm_yday=43, # 今年的第几天
(9) tm_isdst=0 # 时区,如果是1,则表示夏令时
使用
timezone
返回时间标准实际与当前时间差(秒数)In [45]: time.timezone Out[45]: -28800 In [46]: time.timezone / 3600 Out[46]: -8.0 # 中国时间与标准时间相差8小时
altzone
与timezone
差不多
daylight
查看你是否使用了夏令时,0表示没有使用In [50]: time.daylight Out[50]: 0
time ()
返回当前的时间戳In [37]: time.time() Out[37]: 1518407588.8324711
sleep ()
使程序暂停几秒In [51]: time.sleep(3) In [52]:
gmtime ()
传入时间戳,返回格式化元组 【 UTC时区 】In [54]: time.gmtime(time.time()) Out[54]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=4, tm_min=7, tm_sec=50, tm_wday=0, tm_yday=43, tm_isdst=0)
localtime ()
传入时间戳,返回格式化元组(不传入参数,默认当前时间) 【 根据本地区域决定 】In [55]: time.localtime() Out[55]: time.struct_time(tm_year=2018, tm_mon=2, tm_mday=12, tm_hour=12, tm_min=10, tm_sec=13, tm_wday=0, tm_yday=43, tm_isdst=0)
In [56]: time.localtime(123123123) Out[56]: time.struct_time(tm_year=1973, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=52, tm_sec=3, tm_wday=0, tm_yday=330, tm_isdst=0)
取出元组数据:
In [57]: a = time.localtime()
In [58]: a.tm_year # 取出年份
Out[58]: 2018
In [59]: a.tm_mon # 取出月份
Out[59]: 2
In [60]: a.tm_sec # 取出分钟
Out[60]: 54
mktime ()
由元组转换为时间戳In [63]: time.mktime(time.localtime()) Out[63]: 1518409129.0
strftime (format,tuple) --> string
自定义生成格式化时间 -> string- 格式化操作
%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
2. 使用: In [67]: time.strftime(‘%Y-%m-%d‘,time.localtime()) Out[67]: ‘2018-02-12‘
- 格式化操作
ctime ()
传入时间戳,返回文本型时间In [69]: time.ctime() Out[69]: ‘Mon Feb 12 12:30:44 2018‘ In [77]: time.ctime(123123123) Out[77]: ‘Mon Nov 26 08:52:03 1973‘
asctime ()
传入格式化元组,返回文本型时间In [79]: time.asctime(time.localtime(123123)) Out[79]: ‘Fri Jan 2 18:12:03 1970‘
In [80]: time.asctime() Out[80]: ‘Mon Feb 12 12:34:23 2018‘
datetime模块
datetie模块
datetime.now ()
返回当前时间In [156]: print(datetime.datetime.now()) In [156]: 2018-02-12 12:39:43.309016
时间运算
In [174]: datetime.datetime.now() + datetime.timedelta(1)
Out[174]: datetime.datetime(2018, 2, 13, 12, 43, 12, 301435)
In [175]: datetime.datetime.now() - datetime.timedelta(1)
Out[175]: datetime.datetime(2018, 2, 11, 12, 43, 28, 154795)
In [181]: datetime.datetime.now() + datetime.timedelta(hours=1)
Out[181]: datetime.datetime(2018, 2, 12, 13, 44, 54, 816476)
In [182]: datetime.datetime.now() + datetime.timedelta(hours=1)
Out[182]: datetime.datetime(2018, 2, 12, 13, 44, 56, 429732)
原文地址:http://blog.51cto.com/13155232/2071613
时间: 2024-09-30 20:35:16