Reputation: 17876
My code
var d = new Date("2014-09-01");
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
days[d.getDay()]
I expects
days[d.getDay()]
returns Monday but It got Sunday
I am located in Pacific Time California zone
Do I miss something?
Upvotes: 0
Views: 143
Reputation: 1356
Dan Tao is right, here's how you can fix the missing timezone.
var d = new Date("2014-09-01");
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
d.setTime( d.getTime() + d.getTimezoneOffset()*60000 );
http://jsfiddle.net/s1bse8fm/2/
Related: How do you create a JavaScript Date object with a set timezone without using a string representation
Upvotes: 1
Reputation: 128317
The string you're passing to the constructor, "2014-09-01"
does not indicate a time zone. On both Chrome and Firefox (based on my tests right now), this seems to be interpreted as a GMT date. When you call getDay()
, the day is given in your local time zone.
So in my case, since I am in California, the date 2014-09-01
, which is midnight in GMT, is actually 5pm on August 31 in PDT.
Upvotes: 1