Convert UNIX timestamp in jQuery

Jerry Jones picture Jerry Jones · Feb 9, 2013 · Viewed 27.4k times · Source

Well, I fetch timestamp from my table using a php page with getJSON. Here is the structure.

main.php --> using getJSON (abc.php) --> value from my table

Is there any way to convert this UNIX timestamp into this format:

dd-mm-yyyy at hh:mm am

Answer

Konstantin Pozhidaev picture Konstantin Pozhidaev · Feb 9, 2013

The Unix timestamp is the number of seconds elapsed since 1970 (epoch). You would need to convert that to a date object in JS:

var date = new Date(unixTimestamp*1000); // *1000 because of date takes milliseconds

Once you have the date object, you can use any of the techniques mentioned in the following post: How to format a JavaScript date