python time模块时间戳 与 结构化时间详解
作者:侯小啾 时间:2021-04-09 11:06:42
time模块
1:概述
时间表示的分类
时间戳
格式化的时间字符串
结构化时间
时间戳:时间戳表示的是从1970年1月1日整0点到目前秒的偏移量,数据类型是浮点型,主要用来让计算机看的
格式化的时间字符串:如 2019-01-26 等 ,数据类型是字符串,主要是人看
结构化时间:结构化时间会将时间进行分组,比如年,月,日,时,分,秒等等,数据类型是一个元祖,主要做时间转换
导入相关库
import time
1. 时间戳
1.1 time.time()
time.time()可以得到的是 时间戳 。即 1970年1月1日0时0分0秒到现在时间的偏移量 s
t1 = time.time()
print('t1:', t1)
1.2 时间戳 转 字符串
将时间戳转化为 一种固定格式 的字符串,可以使用time.ctime()方法。(但是不太常用)
t = time.ctime(time.time())
print(t)
            
2. 结构化时间
使用结构化时间更便于我们的操作
2.1 本地时间
本地时间与时区有关。
t2 = time.localtime(time.time())
print("t2:", t2)
2.2 格林威治时间(零时区)
格林威治时间(0时区) 比北京时间晚8小时整(全球基准,用这个时间可以消除在不同地方写作代码的影响)
t3 = time.gmtime(time.time())
print("t3:", t3)
2.3 结构化时间 转 字符串
字符串形式是人类一般可以接受的形式
t4 = time.strftime("%Y/%m/%d %H:%M:%S", t2)
print(t4)
print(type(t4))
2.4 字符串 转 结构化时间
上述操作的逆向操作
t5 = time.strptime('2022/06/22 20:30:35', "%Y/%m/%d %H:%M:%S")
print(t5)
2.5 结构化数据的属性
print("年:", t2.tm_year)
print("月:", t2.tm_mon)
print("日:", t2.tm_mday)
print("时", t2.tm_hour)
print("分", t2.tm_min)
print("秒", t2.tm_sec)
print("本周已过天数:", t2.tm_wday)
print("本年已过天数:", t2.tm_yday)
print("是否是夏令时", t2.tm_isdst)
2.6 结构化时间 转 时间戳
t7 = time.mktime(t2)
print(t7)
来源:https://blog.csdn.net/weixin_48964486/article/details/125415019
标签:python,time模块,时间戳
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python编程使用PyQt创建UE蓝图
2023-11-20 14:24:58
![](https://img.aspxhome.com/file/2023/0/64690_0s.gif)
Javascript 每日测试 - 第五期 callee及function
2008-07-10 13:22:00
AJAX应用之草稿自动保存
2007-08-23 08:29:00
Python基础之模块详解
2023-02-24 23:23:52
![](https://img.aspxhome.com/file/2023/2/68432_0s.png)
培养色感的一些经验分享
2023-11-10 03:47:03
![](https://img.aspxhome.com/file/UploadPic/20071/200713110530379s.gif)
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2023-07-17 09:58:42
![](https://img.aspxhome.com/file/2023/0/65540_0s.png)
CREATE FUNCTION sqlserver用户定义函数
2012-08-21 10:57:42
一个很棒的js图片代码
2009-12-17 18:36:00
![](https://img.aspxhome.com/file/UploadPic/200912/17/texiao-10s.gif)
ASPImage组件的实现过程[图]
2008-02-03 15:37:00
![](https://img.aspxhome.com/file/UploadPic/20082/3/200823154516685s.gif)
python3实现带多张图片、附件的邮件发送
2023-05-11 06:51:10
不错的广告定位效果代码
2009-06-05 18:51:00
ASP动态生成的javascript表单验证代码
2008-10-13 20:11:00
按键标示的设计体验
2008-08-27 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20088/27/2008827121557448s.jpg)
asp 去掉html中的table正则代码函数
2011-04-06 10:48:00
getElementsByAttribute
2009-10-27 12:13:00
如何防止页面中的敏感信息被提取
2008-05-04 11:59:00
详解AJAX核心 —— XMLHttpRequest 对象
2010-03-31 14:49:00
iframe的防插与强插
2009-03-03 12:33:00
再谈float菜单局中
2009-12-21 19:57:00
python Tkinter的图片刷新实例
2023-10-31 04:32:24