1

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

Community
  • 1
  • 1
jim dif
  • 611
  • 1
  • 8
  • 17
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date#highlighter_538397 – lanzz Oct 11 '12 at 14:35
  • maybe searching on stack overflow could have helped --> http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript – Samuele Mattiuzzo Oct 11 '12 at 14:35

3 Answers3

0

you could try this

function convert_time(ts) {
   return new Date(ts * 1000) 
}

and call it like so

console.log(convert_time(1349964180));
Fabrizio Calderan
  • 115,126
  • 25
  • 163
  • 167
0

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 the getUTC*() functions to avoid timezone issues.

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
0

A UNIX-timestamp is using seconds whereas JavaScript uses milliseconds. So, you have to multiply the value by 1000:

var myDate  = new Date(1349964180 * 1000);
alert (myDate.toGMTString());
insertusernamehere
  • 22,497
  • 8
  • 84
  • 119