Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I am trying to return a formatted time from a unix time. The unix time is 1349964180
.
If you go to unixtimestamp.com and plug in 1349964180
for Timestamp you will get:
TIME STAMP: 1349964180
DATE (M/D/Y @ h:m:s): 10 / 11 / 12 @ 9:03:00am EST
This is what I want, but in javascript.
So something like:
function convert_time(UNIX_timestamp){
......
......
return correct_format;
}
and then the call:
convert_time(1349964180);
and console.log should print:
10 / 11 / 12 @ 9:03:00am EST
Well, first of all you need to multiply by 1000, because timestamps in JavaScript are measured in milliseconds.
Once you have that, you can just plug it into a
Date
object and return the formatted datetime. You will probably need a helper function to pad the numbers (function pad(num) {return (num < 10 ? "0" : "")+num;}
) and you should use thegetUTC*()
functions to avoid timezone issues.A UNIX-timestamp is using seconds whereas JavaScript uses milliseconds. So, you have to multiply the value by
1000
:you could try this
and call it like so