1
var dateTest = new Date('2015-03-31');
console.log(dateTest);

Result:

Mon Mar 30 2015 20:00:00 GMT-0400 (Eastern Daylight Time)

However I expected this to be March 31. How might I do this?

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • 2
    It takes the date you pass it as UTC, and then applies your local UTC modificator, which is -4, so 2015-03-31 00 - 4h = 2015-03-30 20 – Marco Scabbiolo Apr 27 '16 at 18:31

1 Answers1

3

Because it's setting the date according to UTC, and returning it with your local timezone offset. I'd recommend explicitly specifying the timezone offset, if that's what you need, or adding your timezone offset after the fact.

var d = new Date('2015-03-31T00:00:00-0400');

// or

var d = new Date('2015-03-31');
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());

If you want it to be in UTC, you can call the toUTCString() method on it instead, which will give you the date you expect, albeit not in your timezone.

Brandon Anzaldi
  • 6,884
  • 3
  • 36
  • 55