![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 8
Rep Power: 0
![]() |
algorithm: help needed in difference of dates
lets say i have the date as year, month and day in three variables:-
y1, m1 and d1 and another date in:- y2, m2 and d2 is there any good algorithm to find the difference in the number of days between these two dates. i would be thankful if anyone can provide me a link to any such alogrithm or post a description here. example data:- 14th June 1983: y1=1983, m1=06, d1=14 15th April 2006: y2=2006, m2=04, d2=15 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5
![]() |
I'm not aware of any such calculator offhand, but it wouldn't be hard to whip one up.
30 days hath September, April, June, and November All the rest have 31 Except February clear, which has 28 or 29 (each leap year). In the gregorian calendar, a leap year is every year divisible by 4. But a year divisible by 100 is not a leap year, unless it is also divisible by 400. This should be reasonably accurate for the next 8000 years or so..... when other effects such as the earth's orbit around the sun wobbling a bit will introduce a slight (unfortunately, not totally predictable) error. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 873
Rep Power: 4
![]() |
One way is to convert both dates into a Julian date, which is an integer number of days from a certain date in the past. Here is one method in C for doing so. Yo can then subtract one value from the other to find the number of days between the two dates.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Consider using strptime - which is pretty flexible. Most compilers now support it. Older Windows compilers don't. Most calendric work uses strptime to convert dates to seconds in the epoch, which is the basis for time and date calculations in C.
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* 86400 seconds per day */
#define DAY 86400
/*
lose the
th, nd, st suffixes if they exist
*/
char *remove_th_nd_st(const char *src)
{
const char *values[3]={{"th "}, {"nd "},{"st "} };
char *dest=strdup(src);
char *p=NULL;
int i=0;
while(i<3)
{
if( (p=strstr(src,values[i++]))==NULL ) continue;
if( !isdigit(*(p-1)) ) continue;
p+=3;
sprintf(dest,"%d %s",atoi(src),p);
break;
}
return dest;
}
/* return time difference in days
date1 = recent date
date1 = older date
---otherwise you get a negative value which may be okay.
*/
int date_diff(const char *date1, const char *date2)
{
int retval=0;
const char *fmt="%d %b %Y";
struct tm now, then;
char *wdate1=remove_th_nd_st(date1);
char *wdate2=remove_th_nd_st(date2);
if ( strptime(wdate1,fmt,&now)==NULL ||
strptime(wdate2,fmt,&then)==NULL )
{
perror("Bad date format");
exit(EXIT_FAILURE);
}
now.tm_isdst = -1; /* force mktime to get daylight savings */
then.tm_isdst = -1;
retval=difftime(mktime(&now),mktime(&then));
retval=retval/DAY;
free(wdate1);
free(wdate2);
return retval;
}
int main()
{
char now[32]={0x0};
char then[32]={0x0};
strcpy(then,"14th June 1983");
strcpy(now,"15th April 2006");
printf("Date difference in days: %s - %s = %d\n",
now,then,date_diff(now,then) );
strcpy(now,"1st June 2005");
printf("Date difference in days: %s - %s = %d\n",
now,then,date_diff(now,then) );
strcpy(now,"2nd August 2014");
printf("Date difference in days: %s - %s = %d\n",
now,then,date_diff(now,then) );
strcpy(now,"1st July 1975");
printf("Date difference in days: %s - %s = %d\n",
now,then,date_diff(now,then) );
return 0;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|