3

I have the following timestamp in JS: 1436428916900 When I enter this date in JS I got the following results:

Date(1436428916900) "Tue Feb 16 2016 20:09:42 GMT+0200 (EET)"

I found several questions on SO and most of them offer following solution:

import datetime
my_time = 1436428916900
date = datetime.datetime.fromtimestamp(my_time / 1e3)

But the problem is that this code results in the following result :

datetime.datetime(2015, 7, 9, 11, 1, 56, 900000)

which is absolutely different result from JS code.

Sources : JavaScript timestamp to Python datetime conversion

How to convert integer timestamp to Python datetime

9
  • 1
    The date Python is giving for that timestamp is correct, and date you're getting in JavaScript is wrong - you can confirm this at unixtimestamp.com or using the *nix date utility. What's the exact JavaScript code you're using? Commented Feb 16, 2016 at 18:22
  • Are you saying the dates are wrong? or the formatting? Commented Feb 16, 2016 at 18:22
  • 1
    Try new Date(…) instead of Date(…) in JavaScript. Commented Feb 16, 2016 at 18:23
  • See also stackoverflow.com/questions/3505693/… Commented Feb 16, 2016 at 18:26
  • Date(...)doesn't accept timestamp. It just returns a string representing current time. You'll know - just repeatedly enter Date(1436428916900) quickly and you'll see what I'm saying. new Date(1436428916900) is what you're looking for. Commented Feb 16, 2016 at 18:31

1 Answer 1

5

The JavaScript function Date() is different from the Date constructor invoked by new Date(…). The function silently ignores any parameters and returns a string representing the current time instead of a new Date instance. (See the EcmaScript 2015 Specification for further reference.)

So you should use new Date(timestamp) in JavaScript, and you will get the same result in Python and JavaScript.

JavaScript is quite a special language.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! Didn't think that the problem is in JS code.
"The function only accepts a date string…" is misleading. It also accepts a time value number and objects such as date instances (see ECMA-262 §20.3.2.2).
@RobG What you linked seems to be the documentation of the Date constructor, not of Date() when called as a function. I don't know where the latter is documented in the standard (or whether it's mentioned at all). However, I tried with Firefox, Chrome and NodeJS, and for neither of them Date(timestamp) works in any meaningful way. It always returns a date string referring to the current time, regardless of the value of the timestamp passed in. The only reference I have is the answer I linked.
@RobG: I found the reference in the standard: "When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC)." It always returns the current date, and doesn't accept any argument at all. I'll correct my answer accordingly.
@SvenMarnach—ECMAScript 2015 is more confusing than previous versions in many respects, this is one more. Who would have thought that "Date ( value ) … When the Date function is called…" really means new Date (value) (which is how it was written in previous versions)?

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.