#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    12
    Rep Power
    0

    Unhappy Javascript question about date() and local time


    I'm trying to write a script that takes a UNIX timestamp and returns the user's date and time in his or her local time.

    Here's my code:

    function getLocalTime(GMTtime) {
    var GMTtime = GMTtime * 1000;
    var newDate = new Date(GMTtime);
    return newDate;
    }

    GMTtime is a UNIX timestamp. The first line converts the timestamp to milliseconds, and the second line (theoretically) converts that number into a new date object.

    My understanding is that date() returns a date object in the user's local time, but it's not working that way. It's consistently returning a time three hours ahead of my time (e.g. 3 am if it's midnight here - I'm in California).

    The server is hosted in Montreal, which of course is eastern time zone. Is it possible the script is returning the local time for the server? But javascript is processed client-side, so this shouldn't happen, right?

    I'm totally confused. Any help would be welcome. Thanks!
    Last edited by Subcom; September 19th, 2001 at 01:15 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Tauranga, NZ
    Posts
    349
    Rep Power
    13
    set GMTime to number you know and see if it displays the correct date/time as expected. Then you can narrow the problem down to the javascript or the source for the unix timestamp
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    12
    Rep Power
    0
    I know the UNIX timestamp is accurate (it's generated by the PHP function time()), so the problem is in the javascript somewhere. Any ideas?
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    12
    Rep Power
    0
    Ack! Forget it! Turns out the SERVER TIME was incorrect (three hours ahead). Stupid stupid stupid. Thanks for the help anyway.

IMN logo majestic logo threadwatch logo seochat tools logo