python 字符串转时间戳datetime 以及儒略日的转换

时间:2023-01-19 02:34:57

#coding=utf-8
#设a为字符串
import time
from datetime import datetime
from datetime import timedelta
import time,pdb
a = "20110928100000"
# Now = datetime.now()
#中间过程,一般都需要将字符串转化为时间数组
# pdb.set_trace()
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=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
# timeStamp = 1381419600
dateArray =datetime.utcfromtimestamp(float(time))
print dateArray,type(dateArray)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print otherStyleTime

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

转换时间 按1970 1月1日 开始  多少秒
import time,datetime
timeDateStr="2014-07-29 00:00:00"
time1=datetime.datetime.strptime(timeDateStr,"%Y-%m-%d %H:%M:%S")
secondsFrom1970=time.mktime(time1.timetuple())

 

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python 字符串转时间戳datetime 以及儒略日的转换