A complete datetime increasing algorithm that I wrote:
var d=new Date(); var date=d.toISOString().split('T')[0]; var time=d.toISOString().split('T')[1].split(".")[0]; var yr=parseInt(date.split("-")[0]); var mo=parseInt(date.split("-")[1]); var da=parseInt(date.split("-")[2]); var hr=parseInt(time.split(":")[0]); var min=parseInt(time.split(":")[1]); var sec=parseInt(time.split(":")[2]); console.log("time before"); console.log(`${yr}-${mo}-${da} ${hr}:${min}:${sec}`); //this is the increse time function //first parameter is a string. Possible values: "sec", "min", "hr", "da" //second parameter is an integer. It is the value representing how much you want to increase //here we are increasing time by 52345 minutes... increaseTime("min", 52345); console.log("time after"); console.log(`${yr}-${mo}-${da} ${hr}:${min}:${sec}`); function increaseTime(whichOne, howHuch) { if(whichOne=="sec") sec=sec+howHuch; if(whichOne=="min") min=min+howHuch; if(whichOne=="hr") hr=hr+howHuch; if(whichOne=="da") da=da+howHuch; while(sec>59) { sec=sec-60; min=min+1; }; while(min>59) { min=min-60; hr=hr+1; }; while(hr>23) { hr=hr-24; da=da+1; }; while(da > getNumOfDaysFromMonth(mo, yr) ) { da=da-getNumOfDaysFromMonth(mo, yr); mo=mo+1; while(mo>12) { mo=mo-12; yr=yr+1; }; }; }; function getNumOfDaysFromMonth(mo, yr) { if(mo===1||mo===3||mo===5||mo===7||mo===8||mo===10||mo===12) { return 31; } else if(mo===4||mo===6||mo===9||mo===11) { return 30; } else if(mo===2) { //checking if leap yr if( ((yr%4==0)&&(yr%100!=0))||(yr%400==0) ) return 29; else return 28; } else { console.log("Error in month: "+mo); } };
date2a string or adatetime.datetimeobject?