0

I have already tried out this question but it didn't solve my question.

I have a PHP server which sends a date via JSON to the user where it is then processed by Javascript:

PHP: 'date' => date('D M d Y H:i:s O', strtotime($array['Time']))

Javascript var time = new Date(data.date).toLocaleString()

But instead of getting 18. January 2015 ..., I get 3. March 5877521 -596:-31:-23 GMT+0:53:28. What is wrong there?

Some things you might need to know: The server has the central european timezone as well as the date sent. I am trying (above is only an example) to internationalize the date with javascript.

Community
  • 1
  • 1
Deproblemify
  • 3,340
  • 5
  • 26
  • 41

3 Answers3

5

Why pass a string? JS's date constructor will accept a timestamp:

var time = new Date(<?php echo strtotime($array['Time']) ?>000);

Note the 000 in there. JS uses milliseconds, while strtotime returns in seconds, so effectively you'd be building:

var time = new Date(12345678000);
                    ^^^^^^^^---seconds from php
                            ^^^---instant conversion to milliseconds.
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • then start looking at what `data.date` looks like in your JS code, because there's no way a properly formatted date string would be interpreted as having the year 5877521. or being "negative five hundred and ninety-six o'clock". – Marc B Jan 22 '15 at 18:08
  • I did: Javascript `new Date().getTime() == data.date` is the same (except for the milliseconds, using the timestamp) but if I do new `Date(data.date)` it is wrong. – Deproblemify Jan 22 '15 at 18:14
  • The timestamp in numbers, something like: 1421950569000 – Deproblemify Jan 22 '15 at 18:16
  • I solved it now: `new Date(parseInt(data.date))` works – Deproblemify Jan 22 '15 at 18:18
  • One of my users just reported a similar problem. The server sends a string like "Mon, 07 Mar 2016 17:29:46 UTC +00:00", and the frontend uses JS to display the date: `new Date("Mon, 07 Mar 2016 17:29:46 UTC +00:00").toLocaleString()`. This works fine for me, but my user is reporting that returning a date in March 5877521. – jamesfzhang Mar 07 '16 at 17:34
  • I believe jamesfzhang's user. @MarcB check [this google search](https://www.google.com/search?q=%22March+3%2C+5877521%22) out. It looks like there's a js engine with this bug, and I can't figure out what engine it is. It appears the [Chutzpah test runner](https://mmanela.github.io/chutzpah/) uses it too (which is my issue -- this weird date comes up for any invalid date string when cast to a date in a test, and the test isn't passing b/c an "actual" date (again, this weird March date) is being returned). – ruffin Apr 08 '22 at 20:26
0

I had to parse the timestamp as in Marc B's answer into an int (why ever?):

I solved it now: new Date(parseInt(data.date)) works

Deproblemify
  • 3,340
  • 5
  • 26
  • 41
  • That does not make sense. If you show the actual JSON I am sure there is something weird, like `date:" 1421950569 "` in which case you can use trim instead of parseInt – mplungjan Jan 23 '15 at 06:42
0

First, note that this error is quite common on the web, common enough that it's not [just] a "you" problem.

The reason for the weird March date millions of years in the future seems to be a bug in specific JavaScript engines. I noticed the same date appeared when some tests of ours were run in Chutzpah, a Jasmine test runner for Windows, when our test had an invalid date cast to a locale string and we ran only on its embedded setup from the command line (aka, not in a separate browser).

For our case, it turns out it was PhantomJS that was causing the issue. (PhantomJS used WebKit as its engine. I'd imagine your issue was from an engine with a similar lineage.)

Here's a minimal example that causes the error:

in a file called phantonTest.js

console.log(new Date('').toLocaleString('en-us', { timeZoneName: 'short' }));
phantom.exit();

then execute it...

>phantomjs.exe phantomTest.js
March 3, 5877521 at -8:-31:-23  GMT-4:56:02

You get a slightly date, but are performing a similar operation. You likely have an invalid date coming from strtotime($array['Time']) and the user a similar browser engine. QED, etc.

ruffin
  • 16,507
  • 9
  • 88
  • 138