473,379 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Difference between two dates

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.

4 12263
Luuk
1,047 Expert 1GB
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
9,208 Expert Mod 8TB
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
Sherin
77 64KB
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
9,065 Expert Mod 8TB
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.
Oct 22 '20 #5

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

Similar topics

12
by: Rick | last post by:
I need to store a date prior to the unix epoch, any pointers? -- Rick Digital Printing www.intelligence-direct.com
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
6
by: Ashish Sheth | last post by:
Hi All, In C#, How can I get the difference between two dates in number of months? I tried to use the Substract method of the DateTime class and it is giving me the difference in TimeSpan,From...
3
by: MaRCeLO PeReiRA | last post by:
Hi Guys, I am in troubles with some dates. "I need to know the difference, in days, between two dates." Well, if the difference is less than a month, so I could use:
4
by: Working_Girl | last post by:
Hi, I have a database with insurance clients and their dependents (spouses and children). We had a problem in the past with the twins and some of them have been entered with one month...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
2
by: Blackmore | last post by:
I am trying to use javascript to calculate the difference between two form inputted dates and return the result to another form object. When I load up the page with the function on my web browser...
8
by: Claudia Fong | last post by:
Hi, In VB we have DateDiff to calculate the days difference between 2 dates, I was wondering if we have something like that in C#? I want to calculate for example the days difference...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.