77

To get timestamp in JavaScript we use

var ts = new Date().getTime()

What is the proper way to convert it to a Python datetime so far I use the following code

>>> jsts = 1335205804950
>>> dt = datetime.datetime.fromtimestamp(jsts/1000)
>>> dt
datetime.datetime(2012, 4, 24, 0, 30, 4)

I divide timestamp by 1000 because I get error like

ValueError                                Traceback (most recent call last)
1 d = datetime.datetime.fromtimestamp(a)
ValueError: year is out of range

5 Answers 5

121

Your current method is correct, dividing by 1000 is necessary because your JavaScript returns the timestamp in milliseconds, and datetime.datetime.fromtimestamp() expects a timestamp in seconds.

To preserve the millisecond accuracy you can divide by 1000.0, so you are using float division instead of integer division:

>>> dt = datetime.datetime.fromtimestamp(jsts/1000.0)
>>> dt
datetime.datetime(2012, 4, 23, 11, 30, 4, 950000)
Sign up to request clarification or add additional context in comments.

3 Comments

Thankfully, this isn't necessary in Python 3.
@delnan I have the same issue in python3: datetime.fromtimestamp(1485373592240) fails unless divided by 1000
@jjj I think he meant the float division isn't necessary in python 3.x; we have an explicit integer division operator - // - so if you divide a float by an int with / you get a float.
4

I've had the same issue, thanks to @andrew-clark for the answer, i've build a small example to handle both cases:

     try:
        # when timestamp is in seconds
        date = datetime.fromtimestamp(timestamp)
    except (ValueError):
        # when timestamp is in miliseconds
        date = datetime.fromtimestamp(timestamp / 1000)

Comments

2

For others still getting an error: I had a similar issue but the unix timestamp was in microseconds, i.e. I had to divide the timestamp by 1000000 to get the correct result.

dt = datetime.datetime.fromtimestamp(1502360499615921)

Comments

1

The way you do it is the correct way, because js includes milliseconds in the date/time. Python (and PHP) as far as I know, don't. For more precision you could use /1000.0.

Comments

0

it is a lot of years after this original question, but to anyone wondering, the best approach I found, beyond just dividing by 1000, and to really ensure you have the correct date, as I was getting different results from javascript and python, you need to define UTC as the timezone to get the correct date:

import datetime from datetime
from dateutil import tz

utc_tz = tz.gettz("UTC")

datetime.fromtimestamp(1663804800000/1000, tz=utc_tz)
# datetime.datetime(2022, 9, 22, 0, 0, tzinfo=tzwin('UTC'))

Apparently, javascript new Date(timestamp) will assume UTC for timestamps and I was getting the incorrect date and time when using just datetime:

datetime.fromtimestamp(1663804800000/1000)
# datetime.datetime(2022, 9, 21, 21, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.