The date returned by date picker is off by one day. Is it a problem in my code or is it a bug?
The date sent to date_picker is 2012-03-21. The date returned by dat
After experiencing the same issue and landing on this page, it turned out in my case it was caused by invalid labeling of the days. I started the week on Monday, instead of Sunday. I hope this helps somebody.
var myDate = $.datepicker.parseDate("yy-mm-dd", "2013-10-21");
..//do whatever with myDate now
It is happening due to difference in timezone with date format- yyyy-mm-dd
new Date ('2015/07/10'); // returns: "Fri Jul 10 2015 00:00:00 GMT-0700 (Pacific Daylight Time)"
new Date ('2012-07-10'); // returns: "Thu Jul 09 2015 17:00:00 GMT-0700 (Pacific Daylight Time)"
yyyy/mm/dd
- is not considering timezone while calculating local time.But
yyyy-mm-dd
- is considering time while calculating local time in java script date function.
This can be reproducible when client(browser) and server time zones are different and having timezone/date difference by 1 day.
You can try this on your machine by changing time to different time zones where time gap b/w should be >=12 hours.
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,
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).
After trying many solutions the following code worked for me taken from (https://stackoverflow.com/a/14006555/1736785)
function createDateAsUTC(date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
}
As said Javascript January equals 0 so this would work for datepicker or input type date.
end_date = end_date.split('-');
end_date = new Date(end_date[0],Number(end_date[1])-1,end_date[2]);