♣
题目部分(原文见公众号:python宝)
python宝
https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU5NjIyOTE4OQ==&scene=123#wechat_redirect
小麦苗DB宝
https://www.xmmup.com/
♣
答案部分
在Python中,通常有这几种方式来表示时间:
时间戳:time(),clock()等
格式化的时间字符串:strptime(),asctime()
元组(struct_time)共九个元素:gmtime(),localtime(),strptime()
import time
# 1、此语法中第一个 time 表示 time 模块,该函数不需要传递参数
# 2、返回值:返回当前时间的时间戳
print(time.time()) # 1656986415.2848783
print(time.time()) # 当前国际时间,用新纪元1970开始的秒数表示
# time.asctime([t]))
# 参数 t -- 完整的9位元组元素或 struct_time 表示,由 gmtime() 和 localtime() 函数返回的时间的元组。
# 返回值:此方法返回以下形式的24个字符的字符串: 'Tue Feb 17 23:21:05 2009'.
# 字符串时间 Fri Jul 1 16:14:15 2022
print(time.asctime())
t = (2018, 9, 8, 16, 34, 30, 5, 251, 0)
print(time.asctime(t)) # Sat Sep 8 16:34:30 2018
print(time.asctime(time.localtime())) # Tue Jul 5 10:16:21 2022
# time.localtime([ secs ])
# 参数secs -- 指转换为 time.struct_time 类型的对象的秒数
# 返回值:该函数无任何返回值
# time.struct_time(tm_year=2022, tm_mon=7, tm_mday=1,
# tm_hour=16, tm_min=15, tm_sec=8, tm_wday=4, tm_yday=182, tm_isdst=0)
print(time.localtime()) # 返回元祖
# 国际标准时间,与localtime()相差8h
# 索引(Index) 属性(Attribute) 值(Values)
# 0 tm_year(年) 比如2011
# 1 tm_mon(月) 1 - 12
# 2 tm_mday(日) 1 - 31
# 3 tm_hour(时) 0 - 23
# 4 tm_min(分) 0 - 59
# 5 tm_sec(秒) 0 - 61
# 6 tm_wday(weekday) 0 - 6(0表示周日)
# 7 tm_yday(一年中的第几天) 1 - 366
# 8 tm_isdst(是否是夏令时) 默认为-1
print(time.gmtime())
# 新纪元后的秒数1656663566.0
s = time.localtime()
print(time.mktime(s))
# 自定义格式化:time.strftime(format[, t]),第一个参数是格式化字符串,第二个参数是时间元组。
# 022-07-01 16:24:52
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# Fri Jul 01 16:24:52 2022
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# %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 时区的名字(如果不存在为空字符)
# %% ‘%’字符
About Me:小麦粒
● 本文作者:小麦粒,专注于python、数据分析、数据挖掘、机器学习相关技术,也注重技术的运用
● 作者博客地址:https://blog.csdn.net/u010986753
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● 个人微信号:pythonbao 联系我加微信群
● 个人QQ:87605025
● QQ交流群py_data :483766429
● 公众号:python宝 或 DB宝
● 提供OCP、OCM和高可用最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
如果你觉得到文章对您有帮助,点击下方我的头像,赞赏我哦!
有您的支持,小麦粒一定会越来越好!