2

How do I convert this timestamp from php into a javascript Date() object?

This is how I grab the time:

$timestart = time();

and I parse this to a javascript function and I want to convert it into a JavaScript date object.

help, all this date stuff confuses me quite a bit.

thanks,

dapperwaterbuffalo
  • 2,672
  • 5
  • 35
  • 48
  • possible duplicate of [Convert a Unix timestamp to time in Javascript](http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – Wayne Apr 19 '11 at 20:07
  • generic JS implementations are required to understand RFC-1123 datetime strings. – user422039 Apr 19 '11 at 22:10

3 Answers3

3

If val contains your PHP value which is

the current time measured in the number of seconds since the Unix Epoch

then you just need this:

var timestart = new Date(val * 1000);

JavaScript uses the same base time as UNIX systems (midnight on 01/01/1970) but measured in milliseconds rather than seconds.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

Solution here : Convert a Unix timestamp to time in JavaScript

Community
  • 1
  • 1
john
  • 535
  • 7
  • 16
0

Substring the parts of the timestamp you need to create the Date. Then initialise like so,

var d = new Date(year, month, date);

This is a cross browser implementation.

sciritai
  • 3,688
  • 1
  • 17
  • 22