javascript - Why is .setUTCFullYear() and .setUTCMonth() return different results -


in sample code convert string date:

function stringtodate(){   var edate = "2015-06-01";   logger.log(edate);   var input = edate.split('-');    var date = new date();   date.setutcfullyear(input[0],input[1] - 1,input[2]);   logger.log(date); } 

logging date returns "mon jun 01 20:07:45 gmt+01:00 2015", correct, month '06' - 1 = 5 corresponds month of june this.

however, identical function:

function stringtodate2(){   var edate = "2015-06-01";   logger.log(edate);   var input = edate.split('-');     var date = new date();    date.setutcfullyear(input[0]);   date.setutcmonth(input[1] - 1);   date.setutcdate(input[2]);   logger.log(date); } 

returns "wed jul 01 20:10:04 gmt+01:00 2015". other values return equally screwy results. why different result 'setutcmonth' 'setutcfullyear'?

set date before month

date.setutcfullyear(input[0]); date.setutcdate(input[2]); date.setutcmonth(input[1] - 1); 

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/date/setutcmonth

if parameter specify outside of expected range, setutcmonth() attempts update date information in date object accordingly. example, if use 15 monthvalue, year incremented 1, , 3 used month.

each of utc functions has similar note out of range values.

so because of this:

new date() gets current date, , today being 5/31 date 31.

there months without 31st date, 31 outside range date updated accordingly.

so if try set month february without changing date first, date 2/31/2015, february has 28 days year rolls on 3/03/2015

and in case, if try set june, 6/31/2015, june never has 31st date again rolls on 7/01/2015. , on.

so change date first show above or set default date when create it:

new date("01/01/2015") 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -