2

I have a piece of code which converts user Date into PST however it does not include Daylight saving am i missing something?

any help would be great thank you.

var offset = -8;
var test = new Date(new Date().getTime() + offset * 3600 * 1000).toUTCString().replace(/ GMT$/, "")
alert(test);
Vadim
  • 8,701
  • 4
  • 43
  • 50
Neo
  • 481
  • 2
  • 9
  • 24
  • 1
    You should probably use a library if you want reliable timezone support: http://momentjs.com/timezone/ – Felix Kling Mar 19 '14 at 16:33
  • Whenever I need to deal with dates in JavaScript, I let a date library handle the work for me. Try using something like [moment.js](http://momentjs.com/). – gen_Eric Mar 19 '14 at 16:33
  • @FelixKling but what's wrong with his calculation ? ( it's interesting) – Royi Namir Mar 19 '14 at 16:38
  • 1
    @RoyiNamir: As far as I can tell, nothing. PST is indeed UTC - 8. However, in California we already switched from PST to PDT, which is UTC - 7. Maybe that's what the OP actually wants to compute. – Felix Kling Mar 19 '14 at 16:40
  • @FelixKling but gmt is like utc no ? – Royi Namir Mar 19 '14 at 16:41
  • @RoyiNamir: http://geography.about.com/od/timeandtimezones/a/gmtutc.htm, http://www.diffen.com/difference/GMT_vs_UTC ;) – Felix Kling Mar 19 '14 at 16:42
  • @FelixKling Yes , _GMT) is the same as UTC_ and is the one to use with computers.... – Royi Namir Mar 19 '14 at 16:46

2 Answers2

0

Doing calendars and timezones is hard. Why not save yourself the headaches and look into using something like momentjs, or moment-timezone. If nothing else you might find some inspiration looking through that source code.

Randy L
  • 14,384
  • 14
  • 44
  • 73
0

I would definitely recommend using a library for this sort of thing, an excellent choice is Moment.js.

Link

Using a library like this takes care of things like Daylight saving, leap years etc. In my opinion it's a waste of time doing things like this in your own code, it's really easy to create bugs that often are discovered too late.

See this answer for an example of using Moment.js to do something similar to what you are trying to do: Stack Overflow

Community
  • 1
  • 1
finnmich
  • 121
  • 4