python中时间、日期、时间戳的转换的实现方法
作者:火磷 时间:2022-03-20 13:37:12
1.简介
在编写代码时,往往涉及时间、日期、时间戳的相互转换。
2.示例
# 引入模块
import time, datetime
2.1 str类型的日期转换为时间戳
# 字符类型的时间
tss1 = '2013-10-10 23:40:00'
# 转为时间数组
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print timeArray
# timeArray可以调用tm_year等
print timeArray.tm_year # 2013
# 转为时间戳
timeStamp = int(time.mktime(timeArray))
print timeStamp # 1381419600
# 结果如下
time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
2013
1381419600
2.2 更改str类型日期的显示格式
tss2 = "2013-10-10 23:40:00"
# 转为数组
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
# 转为其它显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print otherStyleTime # 2013/10/10 23:40:00
tss3 = "2013/10/10 23:40:00"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print otherStyleTime # 2013-10-10 23:40:00
2.3 时间戳转换为指定格式的日期
# 使用time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime # 2013--10--10 23:40:00
# 使用datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime # 2013--10--10 15:40:00
2.4 获取当前时间并且用指定格式显示
# time获取当前时间戳
now = int(time.time()) # 1533952277
timeArray = time.localtime(now)
print timeArray
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime
# 结果如下
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)
2018--08--11 09:51:17
# datetime获取当前时间,数组格式
now = datetime.datetime.now()
print now
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime
# 结果如下:
2018-08-11 09:51:17.362986
2018--08--11 09:51:17
通过datetime.datetime.strptime(date_string, format)将原字符串进行时间格式匹配,并赋值给time_format,然后time_format调用strftime(format)函数,输出自己想要的格式
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(0000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00-59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
来源:https://www.cnblogs.com/jfl-xx/p/8024596.html
标签:python,时间,日期,时间戳,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pytorch 状态字典:state_dict使用详解
2023-01-16 11:42:52
python实现在线翻译功能
2023-06-02 22:12:38
如何基于python把文字图片写入word文档
2021-04-20 06:48:49
HTML中事件触发列表与解说
2007-10-22 12:50:00
Python利用Beautiful Soup模块搜索内容详解
2023-10-24 15:53:43
Dreamweaver使用中的7个常见问题与解答
2007-11-03 11:34:00
解读python正则表达式括号问题
2023-08-10 10:22:19
Python中使用tkFileDialog实现文件选择、保存和路径选择
2022-02-17 09:35:32
python之yield和Generator深入解析
2022-09-16 01:25:39
基于matplotlib中ion()和ioff()的使用详解
2021-11-23 17:54:27
Python使用Mediapipe对图像进行手部地标检测
2021-04-23 06:18:52
![](https://img.aspxhome.com/file/2023/5/114745_0s.jpg)
Python实现用手机监控远程控制电脑的方法
2021-06-22 07:57:49
Pytorch1.5.1版本安装的方法步骤
2021-02-07 00:57:09
![](https://img.aspxhome.com/file/2023/5/95235_0s.png)
Python GUI库PyQt5图形和特效样式QSS介绍
2022-05-03 09:25:34
![](https://img.aspxhome.com/file/2023/7/86477_0s.png)
在Internet Explorer中正确使用MSXML
2009-02-22 18:41:00
基于多进程中APScheduler重复运行的解决方法
2021-12-18 05:10:11
python字符串,元组,列表,字典互转代码实例详解
2022-03-01 22:51:55
Python中的二叉树查找算法模块使用指南
2022-02-23 00:51:06
解读HTML:大厦的基石
2008-12-01 12:57:00
实例讲解python函数式编程
2022-10-30 22:14:35