详解python日期时间处理2
作者:雷学委 发布时间:2021-05-20 19:27:15
标签: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))
运行效果如下:
这个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


猜你喜欢
- 目录前言总结:1.代码块的缓存机制2.小数据池3.优缺点小整数对象池大整数对象池字符串驻留机制简单原理:前言本文除"总结"
- 支持实时监控sliderbar的数据,允许有callback回调的函数,有示例1、可自定样式SetStyle() 2、带有onSroll功能
- DB2 存储过程:基础知识您在客户端工作站上对远程服务器和位于该服务器上的数据库进行分类的任何时候,都存在一个简单的 DB2 客户端/服务器
- # -*- coding: cp936 -*-import socketfrom threading import Thread,activ
- 背景:路由结构/video/1.mp4,即/video是父路由,/1.mp4是/video的动态子路由,在/video父路由中会通过url的
- 本文实例讲述了Python图像处理模块ndimage用法。分享给大家供大家参考,具体如下:一 原始图像1 代码from scipy impo
- 之前用Vue+element写了一个后台管理系统,在登录时使用axios请求数据传参时无法正常的获取数据。之后也是一通百度,发现原因是传递参
- 本文实例为大家分享了js实现五子棋游戏的具体代码,供大家参考,具体内容如下html:<body> <h2>五子棋游戏
- 前序、中序和后序表达式是什么?对于像B∗C 这样的算术表达式,可以根据其形式来正确地运算。在B∗
- G2 是蚂蚁金服开源一个基于图形语法,面向数据分析的统计图表引擎。G2Plot 是在 G2 基础上,屏蔽复杂概念的前提下,保留 G2 强大图
- append(),extend(), insert()都是列表操作中常用的插入函数。其中前两个均接收一个参数,并插入到列表尾部。最后一个接收
- 由于自己疏忽,导致请求错误405,然后前端数据传输没错,百度大都说跟post提交方式有关,改成get还是报错,检查才知道,controlle
- 假设我们有一个很简单的OTU表:现在对这个表格进行遍历,一般写法为:import pandas as pdotu = pd.read_csv
- 在程序中,变量就是一个名称,让我们更加方便记忆。cars = 100 space_in_a_car = 4.0 drivers = 30 p
- 二进制日志的文件的作用 mysql二进制日志文件用来记录所有用户对数据库操作,即记录用户对数据库操作的
- 使用的这么长时间的mysql,有一天我突然需要使用mysql 的配置文件my.ini时发现没有这个文件并且这个文件不是被隐藏了。查看自己的m
- 1.网络爬虫的基本概念网络爬虫(又称网络蜘蛛,机器人),就是模拟客户端发送网络请求,接收请求响应,一种按照一定的规则,自动地抓取互联网信息的
- 1.字符串反转使用Python切片反转字符串:# Reversing a string using slicingmy_string = &
- 在 Python 中一切都是对象。如果要在 Python 中表示一个对象,除了定义 class 外还有哪些方式
- 避坑1:RTX30系列显卡不支持cuda11.0以下版本,具体上限版本可自行查阅:方法一,在cmd中输入nvidia-smi查看方法二:由此