Convert between datetime and timestamp

Convert string date and time to 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 

Timestamp converted to string date and time

# 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 

Reference: Add link description

Guess you like

Origin blog.csdn.net/weixin_45281949/article/details/107485480