By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,384 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,384 IT Pros & Developers. It's quick & easy.

Difference between two dates

P: 1
How i can easly calculate the difference between two dates ?

Example :

Date 1 = 12 March(3)
Date 2 = 24 November(11)

Result = 258 days

Can anyone give me an example how to do this in C++ ?
Btw i don't want to use date functions or something like that.I want to do it with simple math formula.Thank you!
Oct 26 '13 #1

✓ answered by weaknessforcats

The easiest thing is to convert each date to a number of days and then subtract the two converted results.

Most date conversion routines have a minimum date where earlier dates are not allowed. I have used Jan 1, 1970 but any date earlier than any date you expect will work. This is your minimum date.

First, subtract the years in the date to be converted from the years in the minimum date to get years. Multiply the years by 365 and add this to the result.

Second divide the years by 4 to get the leap days and add this to the result.

Third, if the year 2000 is between the minimum date years and the date to be converted years, then subtract 1 from the result because years divisible by 400 are not leap years.

Fourth, if the year on the date to be converted is a leap year (divisible by 4 but not by 400) and the month is earlier than March, then subtract 1 from the result because the leap day in this date has not yet occurred.

Fifth, convert the month in the date to be converted to days and add this amount to the result. Use 28 for February since the leap day calculation was done in converting the years.

Sixth, add the days in the date to be converted to the result.

Seventh, get a result for each date and subtract the results.

You may find using a date conversion function is preferable because it is already written and debugged. But do post again and let me know how you fared on this.

Share this Question
Share on Google+
4 Replies

Expert 100+
P: 1,043
when i started to Google,
i found the answer how to do this in 'C' within 5 minutes....

http://latest-technology-guide.blogs...find-date.html

now you can improve your homework skills to convert this 'C' to some 'C++' code
Oct 26 '13 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
The easiest thing is to convert each date to a number of days and then subtract the two converted results.

Most date conversion routines have a minimum date where earlier dates are not allowed. I have used Jan 1, 1970 but any date earlier than any date you expect will work. This is your minimum date.

First, subtract the years in the date to be converted from the years in the minimum date to get years. Multiply the years by 365 and add this to the result.

Second divide the years by 4 to get the leap days and add this to the result.

Third, if the year 2000 is between the minimum date years and the date to be converted years, then subtract 1 from the result because years divisible by 400 are not leap years.

Fourth, if the year on the date to be converted is a leap year (divisible by 4 but not by 400) and the month is earlier than March, then subtract 1 from the result because the leap day in this date has not yet occurred.

Fifth, convert the month in the date to be converted to days and add this amount to the result. Use 28 for February since the leap day calculation was done in converting the years.

Sixth, add the days in the date to be converted to the result.

Seventh, get a result for each date and subtract the results.

You may find using a date conversion function is preferable because it is already written and debugged. But do post again and let me know how you fared on this.
Oct 26 '13 #3

P: 55
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. int valid_date(int date, int mon, int year);
  4.  
  5. int main(void)
  6. {
  7.     int day1, mon1, year1,
  8.         day2, mon2, year2;
  9.  
  10.     int day_diff, mon_diff, year_diff;         
  11.  
  12.     printf("Enter start date (MM/DD/YYYY): ");
  13.     scanf("%d/%d/%d", &mon1, &day1, &year1);
  14.  
  15.     printf("Enter end date (MM/DD/YYYY): ");
  16.     scanf("%d/%d/%d", &mon2, &day2, &year2);
  17.  
  18.     if(!valid_date(day1, mon1, year1))
  19.     {
  20.         printf("First date is invalid.\n");        
  21.     }
  22.  
  23.     if(!valid_date(day2, mon2, year2))
  24.     {
  25.         printf("Second date is invalid.\n");
  26.         exit(0);
  27.     }       
  28.  
  29.     if(day2 < day1)
  30.     {      
  31.         // borrow days from february
  32.         if (mon2 == 3)
  33.         {
  34.             //  check whether year is a leap year
  35.             if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0)) 
  36.             {
  37.                 day2 += 29;
  38.             }
  39.  
  40.             else
  41.             {
  42.                 day2 += 28;
  43.             }                        
  44.         }
  45.  
  46.         // borrow days from April or June or September or November
  47.         else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12) 
  48.         {
  49.            day2 += 30; 
  50.         }
  51.  
  52.         // borrow days from Jan or Mar or May or July or Aug or Oct or Dec
  53.         else
  54.         {
  55.            day2 += 31;
  56.         }
  57.  
  58.         mon2 = mon2 - 1;
  59.     }
  60.  
  61.     if (mon2 < mon1)
  62.     {
  63.         mon2 += 12;
  64.         year2 -= 1;
  65.     }       
  66.  
  67.     day_diff = day2 - day1;
  68.     mon_diff = mon2 - mon1;
  69.     year_diff = year2 - year1;
  70.  
  71.     printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);
  72.  
  73.     return 0; // return 0 to operating system
  74. }
  75.  
  76. // function to check whether a date is valid or not
  77.  
  78. int valid_date(int day, int mon, int year)    
  79. {
  80.     int is_valid = 1, is_leap = 0;
  81.  
  82.     if (year >= 1800 && year <= 9999) 
  83.     {
  84.  
  85.         //  check whether year is a leap year
  86.         if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 
  87.         {
  88.             is_leap = 1;
  89.         }
  90.  
  91.         // check whether mon is between 1 and 12
  92.         if(mon >= 1 && mon <= 12)
  93.         {
  94.             // check for days in feb
  95.             if (mon == 2)
  96.             {
  97.                 if (is_leap && day == 29) 
  98.                 {
  99.                     is_valid = 1;
  100.                 }
  101.                 else if(day > 28) 
  102.                 {
  103.                     is_valid = 0;
  104.                 }
  105.             }
  106.  
  107.             // check for days in April, June, September and November
  108.             else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) 
  109.             {
  110.                 if (day > 30)
  111.                 {
  112.                     is_valid = 0;
  113.                 }
  114.             }
  115.  
  116.             // check for days in rest of the months 
  117.             // i.e Jan, Mar, May, July, Aug, Oct, Dec
  118.             else if(day > 31)
  119.             {            
  120.                 is_valid = 0;
  121.             }        
  122.         }
  123.  
  124.         else
  125.         {
  126.             is_valid = 0;
  127.         }
  128.  
  129.     }
  130.     else
  131.     {
  132.         is_valid = 0;
  133.     }
  134.  
  135.     return is_valid;
  136.  
  137. }
Output:

1st run:
Expand|Select|Wrap|Line Numbers
  1. Enter start date (MM/DD/YYYY): 08/05/2001
  2. Enter end date (MM/DD/YYYY): 08/20/2001
  3. Difference: 0 years 00 months and 15 days.
  4.  
2nd run:

Expand|Select|Wrap|Line Numbers
  1. Enter start date (MM/DD/YYYY): 10/11/2005
  2. Enter end date (MM/DD/YYYY): 05/20/2016
  3. Difference: 10 years 07 months and 09 days.
  4.  
Oct 20 '20 #4

Banfa
Expert Mod 5K+
P: 8,996
You solution doesn't quite answer the question which I think wants the different in days.

Additionally your valid_date function passes 0 and negative values for days as valid.
4 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.