首页
学习
活动
专区
圈层
工具
发布
30 篇文章

六、python学习笔记-模块-time模块

代码语言:javascript
复制
# time模块
"""
1、时间相关的功能模块
"""

代码语言:javascript
复制
# 引入模块
import time
import datetime

# 打印帮助文档
print(help(time))

# 时间戳,返回当前时间的时间戳(1970纪元后经过的浮点秒数)
print(time.time())

# 以科学计数法表示cpu运算时间
print(time.clock())

# 结构化时间,打印格林威治时间(UTC),返回time.struct_time类型的对象(元组格式),(struct_time是在time模块中定义的表示时间的对象)。
# tm_wday表示这周的第几天,周一是0。tm_yday表示今年的第几天。
print(time.gmtime())

# 结构化时间,打印本地时间。
print(time.localtime())

# 字符串时间,自定义格式。单独打印年月日
"""
%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.
"""
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

# 字符串时间转换结构化时间
# 通过 time.strptime截取部分信息
t = time.strptime('2018-04-20 15:30:25', '%Y-%m-%d %H:%M:%S')
print(t)
print(t.tm_wday)

# 默认以字符串打印当前时间。格式已经定义不可以改变。
# 增加时间戳参数(秒),打印时间戳对应的字符串时间。
print(time.ctime())
print(time.ctime(2))

# 将结构化时间转换成时间戳。
print(time.mktime(time.localtime()))

# 以字符串形式打印当前时间
print(datetime.datetime.now())
下一篇
举报
领券