1

This SO post addresses why JavaScript Date constructors can be one day off, but we're seeing output that doesn't account for time zone changes, i.e., it's showing midnight in our time zone (PST).

Any clue what's happening? Ultimately, we want to get the date (no time) in the user's local time zone, add X days, write this new date to localStorage, then fetch the new date. Right now, what we fetch from localStorage is one day behind because of the issue below.

var test = new Date( "Sun Dec 29 2013" )
Sat Dec 28 2013 00:00:00 GMT-0800 (Pacific Standard Time)

Here's a little more detail based on the comments:

var date = new Date(); 
var dateAsString = date.toDateString();
console.log(new Date(dateAsString)); 
Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • If you want it to be a consistent time zone, why not add the time zone when constructing the date object? Adding `GMT` after `Sun Dec 29 2013` will make the date consistent, regardless of the users time zone. In regards to what's happening, Javascript will interpret it on the client side machine, so without a time zone present, it will assume the users. – Robert Dec 29 '13 at 04:01
  • 1
    Okay _what_ version of JavaScript are you using? I'm in that very time zone you are referring to and when I declare the variable that way my response shows `Sun Dec 29 2013 00:00:00 GMT-0800 (PST)`. And why can't you just use ISO 8601? Any reason? – Ray Toal Dec 29 '13 at 04:06
  • @RayToal We're running these commands from the console in Chrome Developer Tools. How do we check the JS version? How can we enforce ISO 8601? Will it work inside a PhoneGap app? – Crashalot Dec 29 '13 at 04:08
  • @Robert, we need the user's time zone because we check things after midnight (their time). – Crashalot Dec 29 '13 at 04:09
  • So then use `.getTimezoneOffset()` to see how long after Zulu midnight you have to wait. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset – Robert Dec 29 '13 at 04:15
  • Are you storing the date as String ("Sun Dec 29 2013")? – Jeff Renaud Dec 29 '13 at 04:16
  • Does `new Date(2013, 11, 29)` make any difference? – Jeff Renaud Dec 29 '13 at 04:21
  • @JeffRenaud, yes we're storing it by calling "toDateString()" first ... should we not? – Crashalot Dec 29 '13 at 04:26
  • Can you try to save the date with `toLocaleDateString()` instead and create a new date with this value (should be something like `new Date('12/29/2013')`. Tell us what you got! – Jeff Renaud Dec 29 '13 at 04:34
  • Ran: new Date( "Sun Dec 29 2013" ).toLocaleDateString(). Output: "12/28/2013". Then tried: var a = new Date( "Sun Dec 29 2013" ).toLocaleDateString(); new Date(a). Output: Sat Dec 28 2013 00:00:00 GMT-0800 (Pacific Standard Time). – Crashalot Dec 29 '13 at 04:41
  • That's so weird! What `console.log(new Date('12/29/2013').toDateString())` gives you? – Jeff Renaud Dec 29 '13 at 04:47
  • To validate your timezone offset, does `new Date().getTimezoneOffset() / 60` gives you 8 (hours)? – Jeff Renaud Dec 29 '13 at 04:50
  • @JeffRenaud, yes we get 8 hours when we run that. we ran, console.log(new Date('12/29/2013').toDateString()), and the output was: Sun Dec 29 2013. – Crashalot Dec 29 '13 at 05:44

1 Answers1

-2

Use the user's time zone offset plus the time string to create a date conforming to the Date Time string format. For example:

var foo = new Date().getTimezoneOffset() / 60;
var bar = new Date().toISOString();
var baz = new Date(bar.replace("Z","+0" + foo + ":00") );

Works for a time zone with a positive, single digit offset.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265