怎么进行python日期时间处理

这篇文章给大家介绍怎么进行python日期时间处理,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

开发中常用的日期操作还有哪些?

  • 时区转换显示

  • 日期格式化

  • 秒数 与 日期 与 字符串的转换

我们经常会用到,比如全球化的业务根据不同客户显示不同时间(格式等)

在python 主要有下面两个模块涵盖了常用日期处理

import time
import calender

我们看看这两个模块。

时间处理中的类型转换:struct_time vs str

Python中创建一个时间,具体来说创建一个struct_time 需要一个9个元素的元组来构造。

asctime 函数帮我们把这种类型的时间格式化为字符串。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
# fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16)
fixed = time.struct_time(the9fields)
print("fixed time:", fixed)
print("type:", type(fixed))
result = time.asctime(the9fields)  # 类似struct_time,需要9个元素构成的元组参数。
print("asc time:", result)
print("type:", type(result))
localtime = time.localtime()
print("local time:", localtime)
print("type:", type(localtime))
print("asc time:", time.asctime(localtime))

运行效果如下:

怎么进行python日期时间处理  python 第1张

这个ticks就是从0时刻计算,至今的秒数累计。

可以隔一秒运行这个程序,每次ticks值加上1(近似)

指定输入来构造时间:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello

import time
#fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16))
print("fixed time:", fixed)

运行效果如下:

怎么进行python日期时间处理  python 第2张

时间与字符串转换

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime2.py
# @Project : hello

import time
sec = 3600  # 纪元开始后的一个小时(GMT 19700101凌晨)
#
gmtime = time.gmtime(sec)
print("gmtime:", gmtime)  # GMT
print("type:", type(gmtime))
print(time.strftime("%b %d %Y %H:%M:%S", gmtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))  # 打印日期加上时区
print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime)  # 本地时间
print("type:", type(localtime))
print(time.strftime("%b %d %Y %H:%M:%S", localtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime))  # 打印日期加上时区
#试试其他格式
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))

下面是运行结果:

怎么进行python日期时间处理  python 第3张

对于时间格式化函数(strftime) 它并不理会你传入的时间(struct_time)是哪个时区的,照样给你输出,也是正确的。

但是我们写程序拿数据的时候,必须把时区信息原样返回到用户端,或者是UI端,最后由客户端本地时区设置进行调整显示。

最后看看,日期文本转换为日期(struct_time).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 7:49 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷学委
# @XueWeiTag: CodingDemo
# @File : createtime4.py
# @Project : hello
import time
print("strptime1:", time.strptime("Jan 01 1970 09:00:00", "%b %d %Y %H:%M:%S"))
print("strptime2:", time.strptime("1970-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))
print("strptime3:", time.strptime("1970-01-01 09:00:00 CST", "%Y-%m-%d %H:%M:%S %Z"))

下面是运行结果:

怎么进行python日期时间处理  python 第4张

关于怎么进行python日期时间处理就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接