C++ program to get previous date of given date

C++ program to find the previous date: this program will take a date and print its previous date.
[Last updated : February 26, 2023]

Getting the previous date of given date

Given a date in DD, MM and YYYY format, you have to find the previous date using C++ program.

Example:
Input: 30/6/2017
Output: 29/6/2017

Many times, we need to find the previous date in C/C++ programming, in this program we are writing code that will print the previous date, here current date or any date whose previous date we have to find.

Logic to get the previous date of a given date in C++

  1. If the day of the month is greater than 1 than its simple to find previous date, just subtract day by 1.
  2. If the day is 1 then we have to check following conditions:
    1. Check the months, which have 31 days in last month, assign 31 to the day and subtract month by 1.
    2. If the month is 3 (March), check the leap year condition and assign 28 or 29 based on year to day and subtract month by 1.
    3. If the month is 1 (January) then assigns 31 to the day and assign 12 (December) to the month, subtract year by 1.
    4. If the month is 2 (February) then assign 31 to the day and subtract month by 1.
    5. And for other months define 30 to the days and subtract month by 1.

C++ code to get the previous date of given date

#include <iostream>
using namespace std;

// fnction to get previous date
void getPrevDate(int* day, int* month, int* year)
{
    // if there is first day of month
    if (*day == 1) {
        // months which have 30 days in previous month
        if (*month == 4 || *month == 6 || *month == 9 || *month == 11) {
            *day = 31;
            *month = *month - 1;
        }
        
        // for MARCH, to define february last day
        else if (*month == 3) {
            if (*year % 4 == 0)
                *day = 29;
            else
                *day = 28;

            *month = *month - 1;
        }
        
        // for January, to define December last day
        else if (*month == 1) {
            *day = 31;
            *month = 12;
            *year = *year - 1;
        }
        
        // for Feb, to define January last day
        else if (*month == 2) {
            *day = 31;
            *month = *month - 1;
        }
        
        // for other months
        else {
            *day = 30;
            *month = *month - 1;
        }
    }
    
    // other days of month
    else {
        *day = *day - 1;
    }
}

int main()
{
    // date, assigning day, month and year
    int dd = 1, mm = 1, yy = 2017;
    
    // printing given date
    cout << endl
         << "Date : " << dd << "/" << mm << "/" << yy;
         
    // function call to get previous date
    getPrevDate(&dd, &mm, &yy);
    
    // printing previous date
    cout << endl
         << "Previous date : " << dd << "/" << mm << "/" << yy;

    return 0;
}

Output

Date : 30/6/2017
Previous date : 29/6/2017


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.