5

When a timestamp is used, for example the fromdate parameter in /questions, what timezone are dates in? Is the number supposed to be in milliseconds or in seconds?

To get the current time to pass as a parameter in JavaScript should I use:

new Date().getTime();

or:

Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);

1 Answer 1

5

The date is Unix Epoch time, which is the number of seconds since 1/1/1970. The "time zone" is UTC, which is nearly equivalent to GMT. You can convert from UTC to local time in javascript using the method outlined here.

Here are the functions I use to convert back and forth (code is C#):

public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp);
}

public static double ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date.ToUniversalTime() - origin;
    return Math.Floor(diff.TotalSeconds);
}
2
  • 2
    +1 although UTC is not a Time Zone but a time standard and Time Zones are expressed as negative or positive offsets from UTC. Commented Aug 9, 2010 at 18:44
  • @system: true dat. Updated. Commented Aug 9, 2010 at 19:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .