Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UNIX timestamp in jQuery

Tags:

jquery

getjson

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

like image 612
Jerry Jones Avatar asked Feb 09 '13 09:02

Jerry Jones


2 Answers

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

like image 120
Konstantin Pozhidaev Avatar answered Oct 12 '22 23:10

Konstantin Pozhidaev


var dt=eval(unixtimestamp*1000);
var myDate = new Date(dt);
return(myDate.toLocaleString());

this will give an output like:10/27/2014, 12:58:45 PM

like image 22
Mahi M. Avatar answered Oct 12 '22 21:10

Mahi M.