Python时间戳使用和相互转换 Python时间戳使用和相互转换详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python时间戳使用和相互转换 Python时间戳使用和相互转换详解

Joe_vv   2021-03-28 我要评论
想了解Python时间戳使用和相互转换详解的相关内容吗,Joe_vv在本文为您仔细讲解Python时间戳使用和相互转换的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python,时间戳,下面大家一起来学习吧。

1.将字符串的时间转换为时间戳

方法:     

import time 
 
a = "2013-10-10 23:40:00" 
 
# 将其转换为时间数组 
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") 
 
# 转换为时间戳 
timeStamp = int(time.mktime(timeArray)) 
 
timeStamp == 1381419600 

2.字符串格式更改

如a = "2013-10-10 23:40:00",想改为 a ="2013/10/10 23:40:00"
方法:先转换为时间数组,然后转换为其他格式

import time 
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") 
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray) 

3.时间戳转换为指定格式日期:

方法一:

利用localtime()转换为时间数组,然后格式化为需要的格式,如

import time 
 
timeStamp = 1381419600 
timeArray = time.localtime(timeStamp) 
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) 
otherStyletime == "2013-10-10 23:40:00" 

方法二:

import datetime 
timeStamp = 1381419600 
dateArray = datetime.datetime.utcfromtimestamp(timeStamp) 
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") 
otherStyletime == "2013-10-10 23:40:00" 

4.获取当前时间并转换为指定日期格式

方法一:

import time 
 
# 获得当前时间时间戳 
now = int(time.time()) 
 
# 转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S" 
timeArray = time.localtime(timeStamp) 
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) 

方法二:

import datetime 
 
#获得当前时间 
now = datetime.datetime.now() #这是时间数组格式 
 
#转换为指定的格式: 
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S") 

5.获得三天前的时间

方法:

import time 
import datetime 
 
# 先获得时间数组格式的日期 
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3)) 
 
# 转换为时间戳: 
timeStamp = int(time.mktime(threeDayAgo.timetuple())) 
 
# 转换为其他字符串格式: 
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") 
 
# 注:timedelta()的参数有:days,hours,seconds,microseconds 

6.给定时间戳,计算该时间的几天前时间:

timeStamp = 1381419600 
 
# 先转换为datetime 
import datetime 
import time 
dateArray = datetime.datetime.utcfromtimestamp(timeStamp) 
threeDayAgo = dateArray - datetime.timedelta(days = 3) 
 
# 参考5,可以转换为其他的任意格式了 

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们