Python 时间戳与时间字符串互相转

时间:2022-06-16 05:44:30
#设a为字符串
import time
a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组
time.strptime(a,'%Y-%m-%d %H:%M:%S')
>>time.struct_time(tm_year=2011, tm_mon=9, tm_mday=27, tm_hour=10, tm_min=50, tm_sec=0, tm_wday=1, tm_yday=270, tm_isdst=-1) #将"2011-09-28 10:00:00"转化为时间戳
time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S'))
>>1317091800.0 #将时间戳转化为localtime
x = time.localtime(1317091800.0)
time.strftime('%Y-%m-%d %H:%M:%S',x)
>>2011-09-27 10:50:00
1.将字符串的时间转换为时间戳
方法:
a = "2013-10-10 23:40:00"
将其转换为时间数组
importtime
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"
方法:先转换为时间数组,然后转换为其他格式
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray) 3.时间戳转换为指定格式日期:
方法一:
利用localtime()转换为时间数组,然后格式化为需要的格式,如
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
otherStyletime == "2013-10-10 23:40:00" 方法二:
importdatetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
otherStyletime == "2013-10-10 23:40:00" 4.获取当前时间并转换为指定日期格式
方法一:
importtime
获得当前时间时间戳
now = int(time.time()) ->这是时间戳
转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) 方法二:
importdatetime
获得当前时间
now = datetime.datetime.now() ->这是时间数组格式
转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S") 5.获得三天前的时间
方法:
importtime
importdatetime
先获得时间数组格式的日期
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
importdatetime
importtime
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray - datetime.timedelta(days = 3)
参考5,可以转换为其他的任意格式了