0

I'm stuck on this problem

to get a user time i would just do

var getUserDateAndTime = new Date();

How would I convert that date time into the PST depending on where they are (EST or GMT) with day light savings in mind.
Any help would be appreciated.

Thank you

Neo
  • 481
  • 2
  • 9
  • 24

1 Answers1

4

You have to get the timezone offset from the client:

var offset = new Date().getTimezoneOffset();

Then you need the PST timezone offset and you can calculate it

ClientTime - ClientOffset + PSTOffset = current PST time

Ra Mon
  • 127
  • 3
  • And how are you factoring in daylight savings here? – scrowler Mar 19 '14 at 00:37
  • 1
    getTimezoneOffset returns a different value in daylight savings, thats why it will work. If you you want to read more about it : [link](http://javascript.about.com/library/bldst.htm) – Ra Mon Mar 19 '14 at 00:56
  • var offset = 420; var pst = new Date() - new Date().getTimezoneOffset() + offset; var currentDate = new Date(pst); wont convert to PST – Neo Mar 19 '14 at 15:59
  • 1
    Here is a short example : http://jsfiddle.net/F5hCX/ you forgot that the offset is in minutes and the time in milliseconds! – Ra Mon Mar 19 '14 at 16:18
  • 1
    Sorry in my example i made a mistake ... you have to know that Date.toUTCString() is returning you (ClientTime - ClientOffset) the current GMT+0 time... so you only have to add PSTOffset. But getTimezoneOffset() returns the value you have to add to your local time to get the GMT+0 time, and you want the other way so you have to subtract the PSTOffset... reworked example : http://jsfiddle.net/F5hCX/1/ – Ra Mon Mar 19 '14 at 16:40