Define date range in CPP

Define date range in CPP, In this program, it will help you in finding the different Date Period. For example, if you wish to find, how many Date have between ” 5th Nov to 8th Nov “. It will represent your Day, Minute, and Second value. Let try this code:-

How do you calculate UTC time in CPP

#include<iostream>
using namespace std;

class Range{
int sd, ed;
int rnge;
int hr, mnt, sec;
public:
void inputrange(){
cout<<"Input the Starting Date Range: ";
cin>>sd;
cout<<"Input the Ending Date Range: ";
cin>>ed;
}
void calculation(){
rnge = ed - sd;
hr = rnge * 24;
mnt = rnge * 1440;
sec = rnge * 86400;
}
void displayoutput(){
cout<<"Number of hours in given Date Range is: "<<hr<<endl;
cout<<"Number of Minutes in given Date Range is: "<<mnt<<endl;
cout<<"Number of Seconds in given Date Range is: "<<sec<<endl;

}
};

int main(){


Range r;
r.inputrange();
r.calculation();
r.displayoutput();

return 0;
}

Output

Input the Starting Date Range: 5

Input the Ending Date Range: 8

Number of hours in given Date Range is: 72

Number of Minutes in given Date Range is: 4320

Number of Seconds in given Date Range is: 259200

How do you find the range in C++

Try this for understanding. It’s a simple program that will show you how actually work given code.

Define date range in CPP
Define date range in CPP
#include <iostream>
using namespace std;

// A date has day 'd', month 'm' and year 'y'

struct Date {
int d, m, y;
};

// To store number of days in
// all months from January to Dec.
const int monthDays[12]
= { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };

// This function counts number of
// leap years before the given date
int countLeapYears(Date d)
{
int years = d.y;

// Check if the current year needs to be
// considered for the count of leap years
// or not
if (d.m <= 2)
years--;

// An year is a leap year if it
// is a multiple of 4,
// multiple of 400 and not a
// multiple of 100.
return years / 4
- years / 100
+ years / 400;
}

// This function returns number of
// days between two given dates
int getDifference(Date dt1, Date dt2)
{
// COUNT TOTAL NUMBER OF DAYS
// BEFORE FIRST DATE 'dt1'

// initialize count using years and day
long int n1 = dt1.y * 365 + dt1.d;

// Add days for months in given date
for (int i = 0; i < dt1.m - 1; i++)
n1 += monthDays[i];

// Since every leap year is of 366 days,
// Add a day for every leap year
n1 += countLeapYears(dt1);

// SIMILARLY, COUNT TOTAL NUMBER OF
// DAYS BEFORE 'dt2'

long int n2 = dt2.y * 365 + dt2.d;
for (int i = 0; i < dt2.m - 1; i++)
n2 += monthDays[i];
n2 += countLeapYears(dt2);

// return difference between two counts
return (n2 - n1);
}

// Driver code
int main()
{
Date dt1 = { 1, 2, 2000 };
Date dt2 = { 1, 2, 2004 };

// Function call
cout << "Difference between two dates is "
<< getDifference(dt1, dt2);

return 0;
}

 

Difference between two dates is 1461

You can easily convert struct to class, here for ease struct is used instead of class to make members public by default instead of private in case of class.

Leave a Reply

x