Javascript Date issues returning 1 day less than set date - timezone -
date formatting issues javscript date().
background: i'm modifying jquery ui datepicker , having trouble months , 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 'd2' return 20160730 when object returns 31st july?
why 'd4' return 20160731 when date set 1st august?
why d3 work correctly?
my assumption that iso date somehow subtracts gmt+10 , gets previous day.
i'm aware of step (protoype function) try , format get string in yyyymmdd format js date object? still mystery me why above produces different results...
return [this.getfullyear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join('');
why 'd2' return 20160730 when object returns 31st july?
because when do:
var d2 = new date(2016,06,31);
you create date equivalent 31 july, 2016 @ 00:00:00 in host system's current time zone. when do:
console.log(d2);
your current system settings used generate string in host system's time zone (apparently gmt+10:00) , show equivalent of 2016-07-31t00:00:00+10:00.
but when do:
console.log('2month: '+d2month+' iso: '+d2iso);
the date in time zone gmt, or 10 hours earlier if time before 10:00, date previous day (gmt) , you'll see:
2016-07-30t14:00:00z
but because sliced time part off string, see date part.
it "works" d3 because when do:
var d3 = new date('2016-07-31');
the date string treated utc*, you're creating date 2016-07-31t00:00:00z equivalent 2016-07-31t10:00:00+10:00, i.e. utc date same local date. note output of:
console.log(d3);
is @ 10:00:00 (because built–in tostring considers system time zone, adds 10 hours displayed date), time zone offset.
in of above, actual time value of date object utc. host system time zone offset used creating time value initially, get , set methods year, month, day, hour, etc. (since daylight saving changes time zone) , when generating human readable date strings.
* note contrary iso 8601, specifies dates without time component should treated local. ecma-262 treats them utc. there versions of browsers '2016-07-31' treated invalid, others treated local, while latest treat utc. why strongly recommended manually parse date strings (use own function or library parser) control how it's parsed , how time zone applied.
Comments
Post a Comment