日期时间datetime与时间戳timestamp互相转换

字符串日期时间转换成时间戳

# '2015-08-28 16:43:37.283' --> 1440751417
def string2timestamp(strValue):     
   d = datetime.datetime.strptime(strValue, "%Y-%m-%d %H:%M:%S") 
   t = d.timetuple() 
   timeStamp = int(time.mktime(t)) 
   timeStamp = float(str(timeStamp) + str("%06d" % d.microsecond))/1000000
   return timeStamp 

时间戳转换成字符串日期时间

# 1440751417 --> '2015-08-28 16:43:37' 
def timestamp2string(timeStamp): 
   d = datetime.datetime.fromtimestamp(timeStamp) 
   str1 = d.strftime("%Y-%m-%d %H:%M:%S") 

   return str1 

参考:添加链接描述

猜你喜欢

转载自blog.csdn.net/weixin_45281949/article/details/107485480
今日推荐