Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Calculating days between two dates Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Laze Ristoski

March 22, 2005, 04:22 PM

This is heavily off-topic I think (and damn boring as well), but I don't know where else to ask. Google returned some online calculators and some tuts about doing this with PHP, and as far as I saw it simply subtracted the dates. But C++ doesn't have a DATE type, and I'm not much into the standard libraries (don't know if they provide something similar), so this poses a difficulty.

I could achieve this by counting every day from the starting date till the ending date (taking leap years into account), but that isn't effective, is it? :P I'd rather do it mathematically.

If someone knows of an online resource or can share some insight, I'd greatly appreciate it.

Thank you.

 
hunguptodry

March 22, 2005, 04:39 PM

julian dates

 
juhnu

March 22, 2005, 08:22 PM

you can have two dates at the same day

 
Patrick Grawehr

March 23, 2005, 02:40 AM

Maybe you calculate a single value from both dates (i.e seconds since some point in time). Most time functions or structures directly offer the possibility to represent the time in a single integer or long value (depending on the required resolution). After that you can subtract the two values from each other and convert the value back to days (by dividing by the number of seconds per day).

 
Chris

March 23, 2005, 04:08 AM

Delphi has these things ready, they calculate the seconds passed since (I think) Jan 1st, 1970. Then it's a simple subtraction.

The only tricky thing is incorporating all those leap seconds artificially introduced since. Leap years can be reasonably calculated (every 4th, every 100th not, every 400th, that's enough).

 
Bramz

March 23, 2005, 04:19 AM

*cough* boost *cough*

:)

Bramz

 
theAntiELVIS

March 23, 2005, 05:19 AM

1/1/1900 is the start date in the default mode. You can change that to go back farther.

 
w3st

March 23, 2005, 05:56 AM

This code calculates the julian day number for a date, it can be used to calculate number of days between two dates, (the day-names in the day array are in swedish)
example:
% julday 2005 03 23 => 2453453 (Onsdag = Wednesday)
% julday 1903 12 18 => 2416466 (Torsdag = Thursday)
2416466-2453453=36987 => Today it is 36987 days after the first flight by Orville Wright.

------------------ julday.c -------------------------------------

  1.  
  2. // from Numerical Recipes in Pascal, page 10
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. char *day[]={"Sondag","Mandag","Tisdag","Onsdag","Torsdag","Fredag","Lordag"};
  8.  
  9. int main(int argc, char **argv)
  10. {
  11.   const int igreg = 588829;
  12.   int ja,jm,jy,jul,jyyy;
  13.   int iyyy,mm,id;
  14.  
  15.   if (argc!=4)
  16.     printf("Usage: %s yyyy mm ddn",argv[0]);
  17.   iyyy=atoi(argv[1]);
  18.   mm=atoi(argv[2]);
  19.   id=atoi(argv[3]);
  20.   if (iyyy<0)++iyyy;
  21.   if (mm>2) {
  22.     jy=iyyy;
  23.     jm = mm+1;
  24.   }
  25.   else {
  26.     jy = iyyy-1;
  27.     jm = mm+13;
  28.   }
  29.   jul = static_cast<int>(365.25*jy) + static_cast<int>(30.6001*jm) + id + 1720995;
  30.   jyyy = iyyy;
  31.   if (id+31*(mm+12*jyyy)>=igreg) {
  32.     ja = static_cast<int>(0.01*jy);
  33.     jul = jul+2-ja+static_cast<int>(0.25*ja);
  34.   }
  35.   printf("julian day #: %d (%s)n",jul,day[(jul+1)%7]);
  36. }
  37.  

 
Christer Ericson

March 23, 2005, 11:07 PM

Laze Ristoski wrote: If someone knows of an online resource or can share some insight, I'd greatly appreciate it.

This site has probably all information you want to know about dates and times:

http://www.merlyn.demon.co.uk/index.htm#dat


Christer Ericson
http://realtimecollisiondetection.net/

 
Laze Ristoski

March 24, 2005, 01:41 AM

Those Julian Dates are very neat. :) And this is a proof that knowing the terminology helps a bunch (read: you know what to search for :)).

Thanks a lot, everyone!

 
Giallanon

March 24, 2005, 03:22 AM

In the following:
gg = day
mm = month
aaaa = year

input: day month year
output: number of days since 1/1/0000

  1.  
  2. int FromDateToDays (int gg,int mm,int aaaa)
  3. {
  4.         // 2000 bistetile
  5.         int ret = gg;
  6.         bool isBisestile = false;
  7.        
  8.         if (aaaa % 4 == 0)
  9.                 isBisestile = true;
  10.  
  11.         for (int i=0;i<(mm-1);i++)
  12.         {
  13.                 switch (i+1)
  14.                 {
  15.                 case 11:
  16.                 case 4:
  17.                 case 6:
  18.                 case 9:
  19.                         ret += 30;
  20.                         break;
  21.  
  22.                 case 2:
  23.                         ret += 28;
  24.                         if (isBisestile)
  25.                                 ret++;
  26.                         break;
  27.  
  28.                 default:
  29.                         ret += 31;
  30.                         break;
  31.                 }
  32.         }
  33.  
  34.         ret += aaaa*365;
  35.         aaaa--;
  36.         while (aaaa >=0)
  37.         {
  38.                 aaaa-=4;
  39.                 ret++;
  40.         }
  41.         return ret;
  42. }
  43.  



*gg = day
*mm = month
*aaaa = year

input = number of days since 1/1/0000
output = day month year
  1.  
  2. void FromDaysToDate (int days,int *gg,int *mm,int *aaaa)
  3. {
  4.         int     a = 0;
  5.  
  6.         while (days > 1461)
  7.         {
  8.                 days -= 1461;
  9.                 a+=4;
  10.         }
  11.         if (days > 366)
  12.         {
  13.                 a++;
  14.                 days -= 366;
  15.                 while (days > 365)
  16.                 {
  17.                         days-=365;
  18.                         a++;
  19.                 }
  20.         }
  21.         *aaaa = a;
  22.  
  23.  
  24.         bool isBisestile = false;
  25.         if (a % 4 == 0)
  26.                 isBisestile = true;
  27.  
  28.         int m;
  29.         for (m=1;m<13;m++)
  30.         {
  31.                 int nGiorni;
  32.  
  33.                 switch (m)
  34.                 {
  35.                 case 11:
  36.                 case 4:
  37.                 case 6:
  38.                 case 9:
  39.                         nGiorni = 30;
  40.                         break;
  41.  
  42.                 case 2:
  43.                         nGiorni = 28;
  44.                         if (isBisestile)
  45.                                 nGiorni++;
  46.                         break;
  47.  
  48.                 default:
  49.                         nGiorni = 31;
  50.                         break;
  51.                 }
  52.  
  53.                 if (days > nGiorni)
  54.                         days-=nGiorni;
  55.                 else
  56.                         break;
  57.         }
  58.         *mm=m;
  59.  
  60.         *gg = days;
  61. }
  62.  

 
Chris

March 24, 2005, 04:16 AM

since 1/1/0000 ?

I cannot believe that. Where did you account for the various calendar reforms ?
It is generally very unreliable to go back beyond ca. 1600.

if (aaaa % 4 == 0)
isBisestile = true;

This is simply wrong.

 
Giallanon

March 28, 2005, 04:29 AM

Show me that aaaa%4==0 is wrong.
And show me that you can find a date that will break the rules I've used.

 
Axel

March 28, 2005, 04:35 AM

http://en.wikipedia.org/wiki/Leap_year#Gregorian_calendar

I would also recommend using boost.

 
Chris

March 28, 2005, 09:35 AM

Giallanon, you really don't know it ?

Look here:
http://www.timeanddate.com/date/leapyear.html

# Every year divisible by 4 is a leap year.
# But every year divisible by 100 is NOT a leap year
# Unless the year is also divisible by 400, then it is still a leap year.

So 2100 for example is not a leap year, even though 2100%4 == 0.

 
Giallanon

March 28, 2005, 10:36 AM

I didn't knew
Thank you :-)

 
Nils Pipenbrinck

March 28, 2005, 11:19 AM

Is there still need for a code, or is the problem already solved?



 
This thread contains 17 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.