3

Mozilla browser I have tried get my time-stamp in JavaScript like strtotime in php

My Code:

//var start_date = data.result[0].start_date;
var start_date = "2011-01-26 13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
// 1296030110

Above code is working fine in chrome. But not working in the Mozilla Browser. I am getting NaN value. Please help me.

After search in google I find one solution to add T between the date and time. so I have added. I am getting the output but the output is not the same in both browser.

var start_date = "2011-01-26T13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
//Mozilla = 1296030110
//Chrome  =  1296044910
Chinmay235
  • 3,236
  • 8
  • 62
  • 93
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – Pranav C Balan Feb 03 '16 at 05:35
  • Also add a timezone signifier after the time, then it will work consistently – Bergi Feb 03 '16 at 05:41
  • It has to be said again: **do not parse strings with the Date constructor**. The format in the OP is not one recognized by ECMAScript 2015 (which is not supported by all browsers in use anyway) so how it is treated is entirely implementation dependent (including as an invalid date). Always manually parse strings, either write your own two line function or use a library. – RobG Feb 03 '16 at 06:03

5 Answers5

5

Do not parse strings with the Date constructor or Date.parse (they do the same thing), it is extremely unreliable, especially for non–standard strings (and some that are). To parse "2011-01-26 13:51:50" as a local time, use a library or a simple function like:

function parseDateTime(s) {
  var b = s.split(/\D/);
  return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5])
}

document.write(parseDateTime("2011-01-26 13:51:50") / 1000);

To include validation an support for missing values adds a bit more code on one more line.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

Try this its work for all browser

start_date="2011-01-26 13:51:50".replace(" ","T");
start_date = new Date(start_date);
var d = start_date.getTime() / 1000;
0
var start_date = "2011-01-26 13:51:50";
var d = Date.now(start_date);
console.log(d);

it will run in mozila you need not to perform any calculation it automatically converts into milliseconds.

abhay vyas
  • 136
  • 2
0

Try this. I am not sure this result is perfect or not.

var start_date = Date("2011-01-26 13:51:50");
var d = Date.parse(start_date) / 1000;
console.log(d);
//1454478429
Developer
  • 2,676
  • 8
  • 43
  • 65
0

this will work

    var start_date = "Jan 26,2011 13:51:50 ";
    var d = Date.parse(start_date)/1000;
    console.log(d);

because

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognised or contains illegal date values (e.g. 2015-02-31).

The parse() method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Gomzy
  • 431
  • 4
  • 14
  • ECMAScript 2015 allows implementations to treat strings that aren't ISO 8601 compliant any way they want (including as invalid dates). It also departs from ISO 8601 in how date only forms are treated, so just because the few implementations you tested parsed the string as you expected doesn't mean they all will. – RobG Feb 03 '16 at 06:16