settingsAccountsettings
Menusettings

Q: Print the next day by given year, month, day in JavaScript

+2 votes

I need to write JavaScript (JS) function that calculates the date of the next day by given year, month and day.

The input comes as three number parameters:

  • The first element is the year
  • The second is the month
  • The third is the day

Examples:

Input:
2016, 9, 30    

Output:
2016-10-1

The output should be returned as a result of your function.

asked in JavaScript category by user sam

1 Answer

+2 votes

Here is the solution my friend:

function calcNextDay(year, month, day) {
    var date = new Date(year, month - 1, day);
    var oneDay = 24 * 60 * 60 * 1000;//86 400 000 milliseconds in one day
    var nextDate = new Date(date.getTime() + oneDay);
    console.log(nextDate.getFullYear() + "-" + (nextDate.getMonth() + 1) + "-" + nextDate.getDate());
}

calcNextDay(2016, 9, 30);

 

answered by user matthew44
edited by user golearnweb
...