时间字符串转时间戳

一、时间字符串转毫秒时间戳

 

import datetime
import calendar

timestr = '2022-06-22T01:07:26.943Z'
timestr = timestr.replace('T',' ').replace('Z','')
print(timestr)  # 2022-06-22 01:07:26.943

fomat_timestr = datetime.datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S.%f')
print(fomat_timestr)  # 2022-06-22 01:07:26.943000

k = calendar.timegm(fomat_timestr.timetuple()) * 1000 + fomat_timestr.microsecond / 1000
print(int(k))  # 1655860046943

 

posted on 2022-06-29 13:45  torotoise512  阅读(951)  评论(0编辑  收藏  举报