Python四种日期时间格式转换:datetime、time、date string、timestamp

     0评论

文章目录[隐藏]

Python处理日期时间有datetime和time两个module,各有相应的object,加上一般的字符串表示法、时间戳,我们在python中至少会见到这四种日期、时间格式。因为关于这些日期时间格式的转换方法总是记不住,于是将所有转换列出来,放到一个脚本中备忘,并将全部转换情形写到AutoHotkey的Hotstring中,以后记不住直接输触发字符就好了。

本文用下面四个缩略写法表示这四种日期时间格式,后面的代码备注、热字符串中都遵循这个规则。

s: date time string
ts: timestamp
do: datetime object
to: time object

一图胜千言,关于这四种格式的转换,看这个很明白了:

需要注意的是,字符串->时间戳datetime->时间戳时间戳->字符串这三种转换没有一步到位的方法,都需要至少两步才能实现。时间戳->字符串的转换可以分别用time、datetime对象作为中间路径,所以都有两种实现。

s2ts -> string to timestamp
do2ts -> datetime to timestamp
ts2s -> timestamp to string # 两种实现

时间戳是没有时区概念的,所以将其转换为其它格式,都提供转换为本地时间与标准时间两种写法。

import datetime import time date_str = "2008-11-10 17:53:59" # s2do dt_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") print("dt_obj: %s [%s]" % (dt_obj, type(dt_obj))) # s2to time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S") print("time_tuple: %s [%s]" % (time_tuple, type(time_tuple))) # s2ts timestamp = time.mktime(time.strptime(date_str, "%Y-%m-%d %H:%M:%S")) # 2 steps print("timestamp: %s [%s]" % (timestamp, type(timestamp))) # do2s date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S") print("date_str: %s [%s]" % (date_str, type(date_str))) # do2to time_tuple = dt_obj.timetuple() print("time_tuple: %s [%s]" % (time_tuple, type(time_tuple))) # do2ts timestamp = time.mktime(dt_obj.timetuple()) # 2 steps print("timestamp: %s [%s]" % (timestamp, type(timestamp))) # to2s date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple) print("date_str: %s [%s]" % (date_str, type(date_str))) # to2do dt_obj = datetime.datetime(*time_tuple[0:6]) print("dt_obj: %s [%s]" % (dt_obj, type(dt_obj))) # to2ts timestamp = time.mktime(time_tuple) print("timestamp: %s [%s]" % (timestamp, type(timestamp))) # ts2do dt_obj = datetime.datetime.fromtimestamp(timestamp) print("dt_obj: %s [%s]" % (dt_obj, type(dt_obj))) dt_obj = datetime.datetime.utcfromtimestamp(timestamp) # 标准时间 print("dt_obj: %s [%s]" % (dt_obj, type(dt_obj))) # ts2to time_tuple = time.localtime(timestamp) print("time_tuple: %s [%s]" % (time_tuple, type(time_tuple))) time_tuple = time.gmtime(timestamp) # 标准时间 print("time_tuple: %s [%s]" % (time_tuple, type(time_tuple))) # ts2s date_str = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") # 2 steps print("date_str: %s [%s]" % (date_str, type(date_str))) date_str = datetime.datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") # 2 steps,标准时间 print("date_str: %s [%s]" % (date_str, type(date_str))) date_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) # 2 steps print("date_str: %s [%s]" % (date_str, type(date_str))) date_str = time.gmtime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) # 2 steps,标准时间 print("date_str: %s [%s]" % (date_str, type(date_str)))

脚本运行结果:

dt_obj: 2008-11-10 17:53:59 [] time_tuple: time.struct_time(tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, tm_isdst=-1) [] timestamp: 1226310839.0 [] date_str: 2008-11-10 17:53:59 [] time_tuple: time.struct_time(tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, tm_isdst=-1) [] timestamp: 1226310839.0 [] date_str: 2008-11-10 17:53:59 [] dt_obj: 2008-11-10 17:53:59 [] timestamp: 1226310839.0 [] dt_obj: 2008-11-10 17:53:59 [] dt_obj: 2008-11-10 9:53:59 [] time_tuple: time.struct_time(tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, tm_isdst=0) [] time_tuple: time.struct_time(tm_year=2008, tm_mon=11, tm_mday=10, tm_hour=9, tm_min=53, tm_sec=59, tm_wday=0, tm_yday=315, tm_isdst=0) [] date_str: 2008-11-10 17:53:59 [] date_str: 2008-11-10 9:53:59 [] date_str: 2008-11-10 17:53:59 [] date_str: 2008-11-10 9:53:59 []

AutoHotkey的热字符串,ts2s有两种,分别是用datetime和time模块,第二种比较简洁些。

::s2do::dt_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") ::s2to::time_tuple = time.strptime(date_str, "%Y-%m-%d %H:%M:%S") ::s2ts::timestamp = time.mktime(time.strptime(date_str, "%Y-%m-%d %H:%M:%S")) ::do2s::date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S") ::do2to::time_tuple = dt_obj.timetuple() ::do2ts::timestamp = time.mktime(dt_obj.timetuple()) ::to2s::date_str = time.strftime("%Y-%m-%d %H:%M:%S", time_tuple) ::to2do::dt_obj = datetime.datetime(*time_tuple[0:6]) ::to2ts::timestamp = time.mktime(time_tuple) ::ts2do::dt_obj = datetime.datetime.fromtimestamp(timestamp) ::ts2to::time_tuple = time.localtime(timestamp) ::ts2s1::date_str = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") ::ts2s2::date_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) ; 标准时间写法,2/to 后面加个u ::ts2udo::dt_obj = datetime.datetime.utcfromtimestamp(timestamp) ::ts2uto::time_tuple = time.gmtime(timestamp) ::ts2us1::date_str = datetime.datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S") ::ts2us2::date_str = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timestamp))

要获取类似Mon, 13 May 2019 10:53:05 GMT格式的时间,用:

datetime.utcfromtimestamp(time.time()).strftime('%a, %d %b %Y %H:%M:%S GMT')

参考资料

本文修订记录

-- EOF --

本文最后修改于6年前 (2019-05-13)

差评不太行一般挺好非常不错 (No Ratings Yet)
读取中...
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址