找出两个日期之间的差异C.

时间:2022-07-13 21:30:14

I am trying to take one date away from another in C and find the difference between them in days. However, this is much more complicated than it first appeared to me as I obviously have to allow for differing days in different years due to leap years and a differing numbers of days depending on which month it is. I did try to use time.h but I do not think that it allows for leap years.

我试图在C中与另一个约会一个日期,并在几天内找到它们之间的差异。然而,这比它最初出现的要复杂得多,因为我显然必须考虑到不同年份的不同日期,因为闰年​​和不同的天数取决于它是哪个月。我确实尝试过使用time.h但我不认为它允许闰年。

Currently my data is stored as integers in an array, for example, {2010, 5, 1, 2011, 6, 1}.

目前,我的数据作为整数存储在数组中,例如{2010,5,1,2011,6,1}。

So could someone please post or point me towards an algorithm that will help me achieve this task? Thank you very much.

那么有人可以发布或指向我的算法,以帮助我完成这项任务吗?非常感谢你。

3 个解决方案

#1


5  

The standard library C Time Library contains structures and functions you want.

标准库C时间库包含您想要的结构和功能。

This header file contains definitions of functions to get and manipulate date and time information.

此头文件包含用于获取和操作日期和时间信息的函数的定义。

Also check C date and time functions and C program days between two dates

还要检查两个日期之间的C日期和时间函数以及C程序天数

EDIT:-

编辑:-

Try this:

尝试这个:

int main()
{
    int day1,mon1,year1,day2,mon2,year2;
    int ref,dd1,dd2,i;
    clrscr();
    printf("Enter first date  day, month, year\n");
    scanf("%d%d%d",&day1,&mon1,&year1);
    printf("Enter second date day, month, year\n");
    scanf("%d%d%d",&day2,&mon2,&year2);
    ref = year1;
    if(year2<year1)
    ref = year2;
    dd1=0;
    dd1=dater(mon1);
    for(i=ref;i<year1;i++)
    {
        if(i%4==0)
        dd1+=1;
    }
    dd1=dd1+day1+(year1-ref)*365;
    dd2=0;
    for(i=ref;i<year2;i++)
    {
        if(i%4==0)
        dd2+=1;
    }
    dd2=dater(mon2)+dd2+day2+((year2-ref)*365);
    printf("\n\n Difference between the two dates is %d days",abs(dd2-dd1));

    getch();
}

int dater(x)
{ int y=0;
    switch(x)
    {
        case 1: y=0; break;
        case 2: y=31; break;
        case 3: y=59; break;
        case 4: y=90; break;
        case 5: y=120;break;
        case 6: y=151; break;
        case 7: y=181; break;
        case 8: y=212; break;
        case 9: y=243; break;
        case 10:y=273; break;
        case 11:y=304; break;
        case 12:y=334; break;
        default: printf("Invalid Input\n\n\n\n"); exit(1);
    }
    return(y);
}

or using time.h try like this:

或使用time.h尝试这样:

#include <stdio.h>  
#include <time.h>       
int main ()
{
  struct tm start_date;
  struct tm end_date;
  time_t start_time, end_time;
  double seconds;

  start_date.tm_hour = 0;  start_date.tm_min = 0;  start_date.tm_sec = 0;
  start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 113;

  end_date.tm_hour = 0;  end_date.tm_min = 0;  end_date.tm_sec = 0;
  end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 113;

  start_time = mktime(&start_date);
  end_time = mktime(&end_date);

  seconds = difftime(end_time, start_time);

  printf ("%.f seconds difference\n", seconds);

  return 0;
}

#2


3  

Algo:

ALGO:

  1. Convert dates to struct tm.
  2. 将日期转换为struct tm。
  3. Convert struct tm to time_t.
  4. 将struct tm转换为time_t。
  5. Take the difference between the time_ts. It yields the difference between dates in seconds.
  6. 取time_ts之间的差异。它产生日期之间的差异,以秒为单位。
  7. Divide the difference in seconds by 86400 to convert it to days.
  8. 将差值除以86400,将其转换为天数。

#3


-1  

/* This function calculates the differance between two dates, passes 6 parameters date-month-year of first date and date-month-year of second date and prints the difference in date-weeks-year  format */
void Date::subtract(int firstDate, int firstMonth, int firstYear,int secondDate, int secondMonth, int secondYear)
{
/*check the dates are valid or not  */
if(isDateValid(firstDate,firstMonth,firstYear) && isDateValid(secondDate,secondMonth,secondYear) )
{

    firstMonth = (firstMonth + 9) % 12;
    firstYear = firstYear - firstMonth / 10;
    FirstNoOfDays = 365 * firstYear + firstYear/4 - firstYear/100 + firstYear/400 + (firstMonth * 306 + 5) /10 + ( firstDate - 1 );

    secondMonth = (secondMonth + 9) % 12;
    secondYear = secondYear - secondMonth / 10;
    SecondNoOfDays = 365 * secondYear + secondYear/4 - secondYear/100 + secondYear/400 + (secondMonth * 306 + 5) /10 + ( secondDate - 1 );

    dayDifference = abs(FirstNoOfDays - SecondNoOfDays); /* uses absolute if the first date is smaller so it wont give negative number */
    years = dayDifference / 365;
    weeks = (dayDifference % 365)/7;
    days = (dayDifference % 365) % 7;
    cout<<years<<" years "<<weeks<<" weeks "<<days<< " days"<<endl;

}
else
{
    cout<<"Invalid Date"<<endl;
}

}

}

#1


5  

The standard library C Time Library contains structures and functions you want.

标准库C时间库包含您想要的结构和功能。

This header file contains definitions of functions to get and manipulate date and time information.

此头文件包含用于获取和操作日期和时间信息的函数的定义。

Also check C date and time functions and C program days between two dates

还要检查两个日期之间的C日期和时间函数以及C程序天数

EDIT:-

编辑:-

Try this:

尝试这个:

int main()
{
    int day1,mon1,year1,day2,mon2,year2;
    int ref,dd1,dd2,i;
    clrscr();
    printf("Enter first date  day, month, year\n");
    scanf("%d%d%d",&day1,&mon1,&year1);
    printf("Enter second date day, month, year\n");
    scanf("%d%d%d",&day2,&mon2,&year2);
    ref = year1;
    if(year2<year1)
    ref = year2;
    dd1=0;
    dd1=dater(mon1);
    for(i=ref;i<year1;i++)
    {
        if(i%4==0)
        dd1+=1;
    }
    dd1=dd1+day1+(year1-ref)*365;
    dd2=0;
    for(i=ref;i<year2;i++)
    {
        if(i%4==0)
        dd2+=1;
    }
    dd2=dater(mon2)+dd2+day2+((year2-ref)*365);
    printf("\n\n Difference between the two dates is %d days",abs(dd2-dd1));

    getch();
}

int dater(x)
{ int y=0;
    switch(x)
    {
        case 1: y=0; break;
        case 2: y=31; break;
        case 3: y=59; break;
        case 4: y=90; break;
        case 5: y=120;break;
        case 6: y=151; break;
        case 7: y=181; break;
        case 8: y=212; break;
        case 9: y=243; break;
        case 10:y=273; break;
        case 11:y=304; break;
        case 12:y=334; break;
        default: printf("Invalid Input\n\n\n\n"); exit(1);
    }
    return(y);
}

or using time.h try like this:

或使用time.h尝试这样:

#include <stdio.h>  
#include <time.h>       
int main ()
{
  struct tm start_date;
  struct tm end_date;
  time_t start_time, end_time;
  double seconds;

  start_date.tm_hour = 0;  start_date.tm_min = 0;  start_date.tm_sec = 0;
  start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 113;

  end_date.tm_hour = 0;  end_date.tm_min = 0;  end_date.tm_sec = 0;
  end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 113;

  start_time = mktime(&start_date);
  end_time = mktime(&end_date);

  seconds = difftime(end_time, start_time);

  printf ("%.f seconds difference\n", seconds);

  return 0;
}

#2


3  

Algo:

ALGO:

  1. Convert dates to struct tm.
  2. 将日期转换为struct tm。
  3. Convert struct tm to time_t.
  4. 将struct tm转换为time_t。
  5. Take the difference between the time_ts. It yields the difference between dates in seconds.
  6. 取time_ts之间的差异。它产生日期之间的差异,以秒为单位。
  7. Divide the difference in seconds by 86400 to convert it to days.
  8. 将差值除以86400,将其转换为天数。

#3


-1  

/* This function calculates the differance between two dates, passes 6 parameters date-month-year of first date and date-month-year of second date and prints the difference in date-weeks-year  format */
void Date::subtract(int firstDate, int firstMonth, int firstYear,int secondDate, int secondMonth, int secondYear)
{
/*check the dates are valid or not  */
if(isDateValid(firstDate,firstMonth,firstYear) && isDateValid(secondDate,secondMonth,secondYear) )
{

    firstMonth = (firstMonth + 9) % 12;
    firstYear = firstYear - firstMonth / 10;
    FirstNoOfDays = 365 * firstYear + firstYear/4 - firstYear/100 + firstYear/400 + (firstMonth * 306 + 5) /10 + ( firstDate - 1 );

    secondMonth = (secondMonth + 9) % 12;
    secondYear = secondYear - secondMonth / 10;
    SecondNoOfDays = 365 * secondYear + secondYear/4 - secondYear/100 + secondYear/400 + (secondMonth * 306 + 5) /10 + ( secondDate - 1 );

    dayDifference = abs(FirstNoOfDays - SecondNoOfDays); /* uses absolute if the first date is smaller so it wont give negative number */
    years = dayDifference / 365;
    weeks = (dayDifference % 365)/7;
    days = (dayDifference % 365) % 7;
    cout<<years<<" years "<<weeks<<" weeks "<<days<< " days"<<endl;

}
else
{
    cout<<"Invalid Date"<<endl;
}

}

}