datepicker date off by one day
This is not a bug, but definitely confusing.
Most of the answers on this page are confused and contain some mis-information.
The real issue is in how the javascript Date
object parses date strings.
The best answer I have found is this stack-O answer. Check out its' excellent write-up.
Below is a very pertinent comment from the answer mentioned above. (credit: @Mizstik)
All of this is due to the behavior of the underlying Date.parse() trying to follow ISO 8601. When the date string follows the yyyy-mm-dd format, it's assumed to be ISO 8601 with implicit UTC 00:00. When the string deviates from the format (e.g. mm-dd-yyyy or slash instead of hyphen), it falls back to the looser parser according to RFC 2822 which uses local time when the timezone is absent. Admittedly, this will all be quite arcane to an average person.
Seems to be a bug. If the string sent to Date()
is formatted as 2012/03/21
instead of 2012-03-21
. The date seems right.
It is not the datepicker,
console.log(new Date('2012-03-21')); //prints Tue Mar 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
The Javascript Date object can accept one of the following syntax as below,
- new Date()
- new Date(milliseconds)
- new Date(dateString)
- new Date(year, month, day [, hour, minute, second, millisecond ])
So in your case it is going to call the dateString and parse. So try appending the time as below,
new Date ('2012-03-21T00:00:00') //should return you Wed Mar 21 2012
DEMO
or Better to use as below,
new Date (2012, 2, 21).
year - Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.
month - Integer value representing the month, beginning with 0 for January to 11 for December.
day - Integer value representing the day of the month (1-31).