Date formatting issues with Javscript Date().
Background: I'm modifying Jquery UI datepicker and am having trouble with months and dates returning incorrect values.
$('#datefield').datepicker({
beforeShowDay: function(date) {
// issues experienced here - isolation test code below
// Mon Aug 01 2016 00:00:00 GMT+1000 (AUS Eastern Standard Time)
// DateMonth: 8 - ISO: 20160731
}
})
Based JS Date() construction on http://www.w3schools.com/jsref/jsref_obj_date.asp
Sample code:
var d1 = new Date();
var d1Month = d1.getMonth()+1;
var d1ISO = d1.toISOString().slice(0,10).replace(/-/g,"");
console.log(d1);
console.log('1Month: '+d1Month+' ISO: '+d1ISO);
var d2 = new Date(2016,06,31);
var d2Month = d2.getMonth()+1;
var d2ISO = d2.toISOString().slice(0,10).replace(/-/g,"");
console.log(d2);
console.log('2Month: '+d2Month+' ISO: '+d2ISO);
var d3 = new Date('2016-07-31');
var d3Month = d3.getMonth()+1;
var d3ISO = d3.toISOString().slice(0,10).replace(/-/g,"");
console.log(d3);
console.log('3Month: '+d3Month+' ISO: '+d3ISO);
var d4 = new Date(2016, 07, 01);
var d4Month = d4.getMonth()+1;
var d4ISO = d4.toISOString().slice(0,10).replace(/-/g,"");
console.log(d4);
console.log('4Month: '+d4Month+' ISO: '+d4ISO);
Output (console):
Wed Aug 24 2016 11:30:51 GMT+1000 (AUS Eastern Standard Time)
1Month: 8 ISO: 20160824
*Sun Jul 31 2016 00:00:00 GMT+1000 (AUS Eastern Standard Time)
2Month: 7 ISO: 20160730
Sun Jul 31 2016 10:00:00 GMT+1000 (AUS Eastern Standard Time)
3Month: 7 ISO: 20160731
*Mon Aug 01 2016 00:00:00 GMT+1000 (AUS Eastern Standard Time)
4Month: 8 ISO: 20160731
Why does 'd2' return 20160730 when the object returns 31st July?
Why does 'd4' return 20160731 when the date set is 1st August?
Why does d3 work correctly?
My Assumption is that that the ISO date somehow subtracts the GMT+10 and gets the previous day.
I'm Aware of this step (protoype a function) to try and format Get String in YYYYMMDD format from JS date object? but its still a mystery to me why the above produces different results...
return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('');