time,datetime,时间戳 时间格式转换

总结:

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")

几个概念

时间戳:整型或float
time对象,datetime对象
字符串 := '2015-06-06 10:10:10'

时间戳<-->struct_time

time_struct = time.localtime(timestamp)
timestamp = time.mktime(time_struct)

struct_time<-->字符串

time.strftime("%Y-%m-%d %H:%M:%S", st)
time.strptime(st, "%Y-%m-%d %H:%M:%S")

时间戳<-->datetime

datetime.datetime.fromtimestamp(ts)
dt.timestamp()

datetime<-->字符串

dt.strftime("%Y-%m-%d %H:%M:%S.%f")
datetime.strptime(dt, "%Y-%m-%d %H:%M:%S.%f")
ps:datetime转time对象:datetimeObj.timetuple()

 

posted on 2017-12-08 16:05  willaty  阅读(543)  评论(0编辑  收藏  举报

导航