Program to print the next day’s date, month, year

Written by

Garvit Gulati

Approaching the problem:

While working with dates we have to keep in mind a variety of cases as months have different number of days. Below is a list of possible cases we have to take care of:

# When day=28 and it’s a February
In this we would have to check if it’s a leap year or not and then set the next date accordingly.

# Month ends of various months
For January, March, May, July, August, October, and December last day is 31. For February it is 28 or 29 depending on it’s a leap year or not. And for rest, it’s 30. So we need to check a combination of month and day before incrementing the month.

# Last day of the year
If its 31st December i.e. last day of the year then the month will be set to 1 and date to 1 and year will be incremented by 1.

Also while printing the date we would have to check if the day and month to be printed are less than 10 as then they will be followed by a zero.

For leap year we will follow the conditions of Georgian Calendar, which states a year is a leap year if:
– It is divisible by 400
– It is divisible by 4 and not divisible by 100

Algorithm:

  1. Accept input in a standardized format, such as DD MM YYYY.
  2. Split the input into separate variables for day, month, and year.
  3. Use a single if-else statement or a switch-case construct to handle all the different cases for the day, month, and year.
  4. Increment the day by 1 if it is less than 28.
  5. For day 28, check if the month is February. If it is, check if it is a leap year. If it is not February, simply increment the day by 1.
  6. For day 29, check if the month is February. If it is, set the day to 1 and increment the month by 1. If it is not February, simply increment the day by 1.
  7. For day 30, check if the month is January, March, May, July, August, October, or December. If it is, increment the day by 1. If it is not, set the day to 1 and increment the month by 1.
  8. For day 31, set the day to 1 and check if the month is December. If it is, increment the year by 1 and set the month to 1. If it is not December, simply increment the month by 1.
  9. Before printing the day and month, check if they need to be preceded by a 0 or not.

Code:

#include <iostream>
using namespace std;

int main() {
  int d, m, y;
  cout << "Enter today's date in the format: DD MM YYYY\n";
  cin >> d >> m >> y;

  if (d > 0 && d < 28) {  // checking for day from 0 to 27
    d += 1;
  }
  if (d == 28) {
    if (m == 2) {  // checking for february
      if ((y % 400 == 0) || (y % 100 != 0 || y % 4 == 0)) {  // leap year check in case of feb
        d = 29;
      } else {
        d = 1;
        m = 3;
      }
    } else {  // when it's not feb
      d += 1;
    }
  }
  if (d == 29) {  // last day check for feb
    if (m == 2) {
      d = 1;
      m = 3;
    } else {
      d += 1;
    }
  }
  if (d == 30) {  // last day check for april, june, september, november
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
      d += 1;
    } else {
      d = 1;
      m += 1;
    }
  }
  if (d == 31) {  // last day of the month
    d = 1;
    if (m == 12) {  // checking for last day of the year
      y += 1;
      m = 1;
    } else {
      m += 1;
    }
  }

  cout << "Tomorrow's date:\n";
  if (d < 10) {  // checking if day needs to be preceded by 0
    cout << "0" << d << " ";
  } else {
    cout << d << " ";
  }
  if (m < 10) {  // checking if month needs to be preceded by 0
    cout << "0" << m << " ";
  } else {
    cout << m << " ";
  }
  cout << y;
  return 0;
}

Output:

Enter today's date in the format:DD MM YYYY
28 02 2020

Tomorrow's date:
01 03 2020
Program to print the next day’s date, month, year