0

I have a code that makes an ajax call from a php file. This php file returns some data from the database as json. The data contains a column with timestamp type and this how it looks :

2015-10-24 18:04:28

but I want to convert this format to look like this :

1445724268000

I tried to use Date.parse() but when I log.console it I'm getting NaN

3
  • Why not var timestamp = new Date('2015-10-24 18:04:28').getTime(); ? Commented Jan 8, 2016 at 18:25
  • @GökayGürcan This won't work on every browsers as this date string is not a perfectly valid date format. Will work on Chrome, but not on Firefox. Commented Jan 8, 2016 at 19:32
  • Ah, indeed. I didn't understand browser related restriction when I read the OP. My bad clearly, thanks again! Commented Jan 9, 2016 at 14:03

4 Answers 4

2

"2015-10-24 18:04:28" is not a valid parameter value for either Date.parse() or new Date(). Different browsers may interpret nonstandard values differently, so it might work for some people even though it's failing for you, depending on what browsers are being used.

According to the ECMAScript 2015 spec, the date string should be a simplification of the ISO 8601 extended format: YYYY-MM-DDTHH:mm:ss.sssZ (such as "2015-10-24T18:04:28").

If you can replace the space character between the date and the time with the character T, your string should become an acceptable date string.

Here's a working example:

var inputs = document.querySelectorAll("input");
var outputs = document.querySelectorAll("span");

for(var i = 0; i < inputs.length; i++){
  (function(index){
    inputs[index].addEventListener("keyup",function(){updateOutput(index);});
  })(i);
  updateOutput(i);
}

function updateOutput(index){
  var date = new Date(inputs[index].value);
  outputs[index].innerHTML = date.getTime();
}
<input type="text" value="2015-10-24 18:04:28" id="inputOne"/><span id="outputOne"></span> (works in Chrome, could fail in IE and Firefox)<br/>
<input type="text" value="2015-10-24T18:04:28" id="inputOne"/><span id="outputOne"></span> (works in all browsers)<br/>
<input type="text" value="October 24, 2015 18:04:28" id="inputOne"/><span id="outputOne"></span> (this usually works too, but isn't in the spec)

Note: Some sources (MDN) also indicate that a date string can be an IETF-compliant RFC 2822 timestamp (such as "October 24, 2015 18:04:28") but this is not part of the actual spec, and might not be supported in future JavaScript implementations.

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

Comments

1

Since timestamp received is a string,replace the space with T:

var timestampString = "2015-10-24 18:04:28"; //Your input

You just need to to convert it into a valid date format and use getTime() method.

var timestamp = new Date(timestampString.replace(' ','T')).getTime();

Comments

0

Date.getTime() returns the epoch string (for example, 757382400000)

So to convert your timestamp you can write:

var date = new Date(timestamp);
var epochString = date.getTime();

IF you output to the console (console.log(epochString), you'll end up with something like 757382400000.

3 Comments

when I tried that I got Date {Invalid Date} in console
Not all browsers can parse date when date is presented in this string format. Works in Chrome, but won't work on Firefox.
@AniketSinha oh. Just tried it. Thanks for the info!
0

If the above format is constantly used, I would do something like this:

function formatDate(myFormat) {
  var dateTime = myFormat.split(" ");
  var myDate = new Date (dateTime[0]);
  var myTime = dateTime[1].split(":");   
  var result = new Date(
   myDate.getYear() + 1900, 
   myDate.getMonth(), 
   myDate.getDate(),
   myTime[0], 
   myTime[1],
   myTime[2], 
   0);
  return result;
}

var date = formatDate("2015-10-24 18:04:28");
alert(date.getTime());

This gives you the local date and time. You may want to switch to UTC values.

See http://www.w3schools.com/js/js_dates.asp for more information about dates.

Here's a JSFiddle.

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.