Reputation: 14038
var date = new Date('2013-04-15');
console.log(date);
Outputs:
Sun Apr 14 2013 20:00:00 GMT-0400 (EDT)
Which is -1 Day, why does Date have this behavior?
Upvotes: 0
Views: 1215
Reputation: 17597
Because new Date()
is using UTC time, the toString()
use your current timezone.
If you want to print the UTC time, you should use
var date = new Date('2013-04-15');
console.log(date.toUTCString());
Upvotes: 0
Reputation: 97571
These two timestamps represent the same time:
Sun Apr 14 2013 20:00:00 GMT-0400 (EDT)
Mon Apr 15 2013 00:00:00 UTC
You're getting the first, but expecting the second. The date constructor appears to take a time in UTC.
If you do:
var date = new Date('2013-04-15 EDT');
console.log(date);
Then you'll probably get the intended result
Edit: This behavior is bizarre. This code works as you intend it to:
var date = new Date('Apr 15 2013');
console.log(date);
Mon Apr 15 2013 00:00:00 GMT+XYZ
Upvotes: 2
Reputation: 13501
You need to specify the timezone to get the output you're looking for.
An example here: Javascript date object always one day off?
Upvotes: 1