1

In Python 2 how to convert 10 digit timestamp to 13 digit timestamp?

Input:

today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)
today_unixtime = int((mod_time.mktime(today.timetuple())+today.microsecond/1000000.0))
dt_object = datetime.fromtimestamp(today_unixtime)
print(today)
print("timestamp =", today_unixtime)
print("date =", dt_object)

Output:

2020-01-17 11:31:40.450943
('timestamp =', 1579240900)
('date =', datetime.datetime(2020, 1, 17, 11, 31, 40))

I am getting correct date and time. But when I checked in Chrome browser console,

new Date(1579240900)

it's given me 50 years back date. While I am converting same timestamp in Python:

Input:

 dt_object = datetime.fromtimestamp(today_unixtime)

Output:

 Mon Jan 19 1970 12:10:40 GMT+0530 (India Standard Time)

Timestamp has to be always 13 digit in year of 2020. Why it's coming as 10 digit? How to convert as 13 digit in Python 2?

I need 13 digit only, my API is taking 13 digit.

This question also asked python convert 10 digits datetimestamp to 13 digit GMT timestamp but no answers.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Atique Ahmed
  • 308
  • 4
  • 16
  • 3
    Multiply it by 1,000? JavaScript handles timestamps in *milli*seconds. – jonrsharpe Jan 17 '20 at 08:03
  • In fact, *your answer* on the linked question shows multiplying by 1,000, so I'm baffled as to what you're asking here. – jonrsharpe Jan 17 '20 at 08:09
  • @jonrsharpe Stackoverflow allows to answer your own question if you think it is rellevant for other users. So the OP doesn't really have a question, just wants to share how to solve a problem other people might encounter –  Jan 17 '20 at 09:27
  • @SembeiNorimaki [self answered questions](https://stackoverflow.com/help/self-answer): 1. must still meet the quality requirements of all posts; and 2. generally don't have the answer posted *an hour later*. Also the OP here has clearly been able to find the previous question, so no signpost is needed, and explicitly says *"but no answers"* despite having posted the same thing there. Maybe that *is* what they were trying to do, but they've gone about it in a baffling way (and it's still just a trivial multiplication, not to mention a dupe of https://stackoverflow.com/q/5998245/3001761). – jonrsharpe Jan 17 '20 at 09:30

1 Answers1

0

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.

Atique Ahmed
  • 308
  • 4
  • 16