您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:详解python日期时间处理2

51自学网 2022-02-21 10:45:16
  python
这篇教程详解python日期时间处理2写得很实用,希望能帮到您。

前篇我们稍微学习了Python中时间的获取,这次继续学习日期的时区转换,格式化等等。

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

  • 时区转换显示
  • 日期格式化
  • 秒数 与 日期 与 字符串的转换

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

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

import timeimport 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 : helloimport 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))

运行效果如下:

屏幕快照 2021-11-12 上午12.14.14.png

这个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 : helloimport 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)

运行效果如下:

时间与字符串转换

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2021/11/10 22:49 上午# @Author : LeiXueWei# @CSDN/Juejin/Wechat: 雷学委# @XueWeiTag: CodingDemo# @File : createtime2.py# @Project : helloimport timesec = 3600  # 纪元开始后的一个小时(GMT 19700101凌晨)#gmtime = time.gmtime(sec)print("gmtime:", gmtime)  # GMTprint("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))

下面是运行结果:

对于时间格式化函数(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 : helloimport timeprint("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 日期处理挺多把戏的,换个格式打印/转换,结果就不一样了。

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注51zixue.net的更多内容!


Python线程之多线程展示详解
Pygame实现监听鼠标示例详解
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。