详解python日期时间处理2
作者:雷学委 时间:2021-05-20 19:27:15
前篇我们稍微学习了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))
运行效果如下:
这个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)
运行效果如下:
时间与字符串转换
#!/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))
下面是运行结果:
对于时间格式化函数(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"))
下面是运行结果:
来源:https://levin.blog.csdn.net/article/details/121551375
标签:python,日期,时间,处理


猜你喜欢
跟老齐学Python之Import 模块
2022-02-02 21:13:34

谈谈XHTML中CDATA
2007-09-17 12:45:00
Django 批量插入数据的实现方法
2023-01-11 10:40:58
Python开发工具Pycharm的安装以及使用步骤总结
2022-09-15 08:21:01

Python视频编辑库MoviePy的使用
2022-12-22 21:07:07

python pillow模块使用方法详解
2021-12-30 14:55:34
python 计算方位角实例(根据两点的坐标计算)
2023-08-01 09:30:54
windows 7安装ORACLE 10g客户端的方法分享
2012-07-11 15:36:18
Python学习之元组的使用详解
2021-05-13 10:49:23

python爬虫看看虎牙女主播中谁最“顶”步骤详解
2022-03-31 09:30:33

golang如何通过viper读取config.yaml文件
2023-07-22 05:46:11

Python时间操作之pytz模块使用详解
2023-05-10 02:57:17

对于任意的XML的遍历
2008-09-05 17:11:00
全民学编程之 Hello World
2023-03-13 20:48:37
CentOS7安装MySQL8的超级详细教程(无坑!)
2024-01-25 13:25:41

python使用正则表达式匹配字符串开头并打印示例
2021-07-02 00:52:13
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
2023-08-24 00:25:21

如何快速定位页面中复杂 CSS BUG 问题
2009-01-15 12:23:00
python实现打砖块游戏
2023-11-09 21:27:15
关于 SQL Server ErrorLog 错误日志说明
2024-01-19 23:57:03