python convert 10 digits datetimestamp to 13 digit GMT timestamp [duplicate]

為{幸葍}努か 提交于 2020-01-24 17:52:44

问题


I receive a timestamp from a server like this 1512543958 & when i send back requests in headers i see a 13 digit GMT time stamp like this 1512544485819

By changing the time to local using the code below i get 2017-12-06 12:35:58

print(datetime.datetime.fromtimestamp(int("1512544474")).strftime('%Y-%m-%d %H:%M:%S')

& when i apply the code below i get 'Wed 06 Dec 2017 07:14:45 GMT'

time.strftime("%a %d %b %Y %H:%M:%S GMT", time.gmtime(1512544485819 / 1000.0))

So basically i need a python function that takes the 10 digit date timestamp as argument & returns 13 digit GMT date timestamp

example input 1512544474
expected output 1512544485819


回答1:


I am using python 2.7

import time
import datetime
import time as mod_time
from datetime import datetime
from datetime import datetime, timedelta
from datetime import date, timedelta  

today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)
today_time = int((mod_time.mktime(today.timetuple())))
yesterday_time = int((mod_time.mktime(yesterday.timetuple())))
today_unixtime = (today_time*1000)
yesterday_unixtime = (yesterday_time*1000)
print("today timestamp =", today_unixtime)
print("yesterday timestamp =", yesterday_unixtime)

Output

('today timestamp =', 1579243097000)
('yesterday timestamp =', 1579156697000)

You can check the current time stamp from crome browser console.

new Date(1579243097000)

it will gives you a current date and time.



来源:https://stackoverflow.com/questions/47668974/python-convert-10-digits-datetimestamp-to-13-digit-gmt-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!