简体   繁体   中英

Convert the format of date in javascript

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

"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.

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();

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 .

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM