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
  • 641
  • 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
  • 120,726
  • 26
  • 164
  • 177
  • Thu Oct 11 2012 10:32:00 GMT-0400 (Eastern Daylight Time) <-- This is very close I just need the 9:03:00am EST and not the GMT-0400 – jim dif Oct 11 '12 at 14:40
  • see the accepted answer here: http://stackoverflow.com/questions/7337327/find-current-time-in-et-est-or-edt-as-appropriate – Fabrizio Calderan Oct 11 '12 at 14:54
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
  • 320,036
  • 81
  • 464
  • 592
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
  • 23,204
  • 9
  • 87
  • 126