Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Epoch Date to meaningful Javascript date

I am getting a string value "/Date(1342709595000)/"in the JSON. I am trying to extract the digits alone and convert the epoch date to meaning ful Javascript Date in the format mm/dd/yy hh:mm:ss . I was able to achieve the first part of the question extracting the digits but couldnot convert it to date object human readable format as available in http://www.epochconverter.com/

JS Fiddle: http://jsfiddle.net/meetravi/QzKwE/3/

like image 317
Ravi Avatar asked Jul 19 '12 16:07

Ravi


People also ask

How do I convert epoch to date?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.


1 Answers

There is nothing you really need to do, they are already milliseconds since epoch and javascript dates take milliseconds since epoch.

http://jsfiddle.net/QzKwE/9/

var dateVal ="/Date(1342709595000)/";
var date = new Date(parseFloat(dateVal.substr(6)));
document.write( 
    (date.getMonth() + 1) + "/" +
    date.getDate() + "/" +
    date.getFullYear() + " " +
    date.getHours() + ":" +
    date.getMinutes() + ":" +
    date.getSeconds()
);

like image 137
Esailija Avatar answered Sep 30 '22 09:09

Esailija