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

Community
  • 1
  • 1
ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
  • 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 http://www.unixtimestamp.com or using the *nix date utility. What's the exact JavaScript code you're using? – Ben Feb 16 '16 at 18:22
  • Are you saying the dates are wrong? or the formatting? – phenxd Feb 16 '16 at 18:22
  • 1
    Try `new Date(…)` instead of `Date(…)` in JavaScript. – Sven Marnach Feb 16 '16 at 18:23
  • See also http://stackoverflow.com/questions/3505693/difference-between-datedatestring-and-new-datedatestring – Sven Marnach Feb 16 '16 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. – Cajetan Rodrigues Feb 16 '16 at 18:31
  • @SvenMarnach—what difference do you expect that will make? – RobG Feb 16 '16 at 22:51
  • @CajetanRodrigues—the [*ECMAScript Language Specification*](http://www.ecma-international.org/ecma-262/6.0/#sec-date-value) disagrees with you. When *Date* is called as a function with a single value that is Type number, it is assumed to be a time value. The function returns an implementation dependent string for the date generated from the value. It also accepts numbers and objects. – RobG Feb 16 '16 at 22:58
  • @RobG got it, thanks for the pointer. – Cajetan Rodrigues Feb 17 '16 at 03:23
  • @RobG The difference I expect is that `new Date(timestamp)` works as expected, while `Date(timestamp)` doesn't. – Sven Marnach Feb 17 '16 at 14:09

1 Answers1

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.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Thanks! Didn't think that the problem is in JS code. – ig-melnyk Feb 16 '16 at 19:01
  • "*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*](http://www.ecma-international.org/ecma-262/6.0/#sec-date-value)). – RobG Feb 17 '16 at 02:49
  • @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. – Sven Marnach Feb 17 '16 at 13:57
  • 1
    @RobG: I found the [reference in the standard](http://www.ecma-international.org/ecma-262/6.0/#sec-date-constructor): "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. – Sven Marnach Feb 17 '16 at 14:05
  • @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)? – RobG Feb 18 '16 at 02:00