I have an API call that returns "/Date(1425715200000)/" as part of a JSON object. I need to be able to evaluate this and return the human readable date. The API documentation is scant at best. I am not even certain why they don't just store the time in milliseconds as an integer.
Not sure what the best way to convert and evaluate /Date(1425715200000)/ this to output 3/7/15.
Used this to replace the "/" characters.
x = x.replace(/\//g,'');
You will have to add in custom code to parse your time in milliseconds dates and create a new Date() object from it. Then extract the month day and year properties of the date object.
Heads up on the .getMonth as it will return a value from 0-11.
var jsonObj = { date :"/Date(1425715200000)/" } function parseDateString( dateObjString ){ var startIndex = dateObjString.indexOf("(") + 1; var endIndex = dateObjString.indexOf(")"); var timeInMilli = dateObjString.substring( startIndex, endIndex); var displayDate; var date = new Date(); date.setTime( timeInMilli ); displayDate = ( date.getMonth() + 1 ) + "/" + date.getDate() + "/" + date.getFullYear(); alert( displayDate ); } parseDateString( jsonObj.date );
With this sample, you will retrieve a Date
instance that you can display as you want:
var result = "/Date(1425715200000)/"; result = result.replace(/\\/Date\\((\\d*?)\\)\\//, "$1"); result = new Date(1 * result); document.getElementById("output").textContent = result;
<span id="output"></span>
Use regex to extract the timestamp, then create an instance of the Date class.
var result = { date: "/Date(1425715200000)/" }; var ts, parts = result.date.match(/\\/Date\\(([0-9]+)\\)\\//); if (parts && parts.length == 2) { ts = parseInt(parts[1]); } if (ts) { var date = new Date(ts); var str = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear(); console.log(str); document.getElementById("output").textContent = str; }
<span id="output"></span>
A little more detail to my comment...
var test = "\\Date(1425715200000)\\";
var time = test.match(/\d+/)[0];
var intTime = parseInt(time);
var date = new Date(intTime);
console.log(date); // Sat Mar 07 2015 02:00:00 GMT-0600 (CST)
console.log((date.getUTCMonth() + 1) + "/" + date.getUTCDate() + "/" + date.getUTCYear()); // 03/07/14
Try the following:
var dt = new Date(parseInt('/Date(1425715200000)/'));
var month = dt.getMonth()++, date = dt.getDate(), year = dt.getYFulllYear();
console.log(month+'/'+date+'/'+year.substr(2));
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.