C语言 日期比较以查找 C 中哪个更大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5283120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:02:17  来源:igfitidea点击:

Date comparison to find which is bigger in C

c

提问by Vivekanandan

I want to know how to find which is bigger date using a c program

我想知道如何使用 ac 程序找到哪个日期更大

kindly help me out plz....

请帮我解决lz....

Thanks

谢谢

回答by Jo?o Silva

You can use the difftimefunction:

您可以使用该difftime功能:

#include <time.h>
#include <stdio.h>

int main(void) {
  time_t date1, date2;
  // initialize date1 and date2...

  double seconds = difftime(date1, date2);
  if (seconds > 0) {
    printf("Date1 > Date2\n");
  }

  return 0;
}

If your dates are not of type time_t, you can use the function mktimeto convert them.

如果您的日期不是 type time_t,您可以使用该函数mktime来转换它们。

回答by SharpUrBrain

#include <stdio.h>

struct date 
{
   int month;
   int date;
   int year;
};


int main(void)
{
    int i=compare_dates (struct date d1, struct date d2);
    switch(i)
    {
       case -1:
         printf("%d/%d/%d is earlear date than %d/%d %d", D1.day, D1.month, D1.year, D2.day
       case 1: 
         printf("%d/%d/%d is later date than %d/%d/%d",D1.day,D1.month,D1.year,D2.day…
       case 0: 
         printf("%d/%d/%d is the same date than %d/%d/%d", D1.day, D1.month, D1.year, D2.day
     }
   return 0;
}


int compare_dates (struct date d1, struct date d2)
{
    if (d1.year < d2.year)
       return -1;

    else if (d1.year > d2.year)
       return 1;

    if (d1.year == d2.year)
    {
         if (d1.month<d2.month)
              return -1;
         else if (d1.month>d2.month)
              return 1;
         else if (d1.day<d2.day)
              return -1;
         else if(d1.day>d2.day)
              return 1;
         else
              return 0;
    }
}

回答by Sylvain Defresne

Can you give more information about what you want to achieve ? Because comparing date is really easy. After all, they are just number of seconds (or milli, micro, nano, ...) since a given past date, or a structure containing year, month, day, ... Whatever the format, the comparison should be pretty easy to perform.

你能提供更多关于你想要实现的目标的信息吗?因为比较日期真的很容易。毕竟,它们只是自给定的过去日期以来的秒数(或毫、微、纳……),或者包含年、月、日等的结构……无论格式如何,比较都应该很容易去表演。

Maybe you want to compare two date given by the user as strings (something like "2011-03-12 18:38") ? Then, you can use strptimeto convert the string to a struct tm, and then do the comparison.

也许您想比较用户作为字符串给出的两个日期(类似于“2011-03-12 18:38”)?然后,您可以使用strptime将字符串转换为 a struct tm,然后进行比较。

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int parse_date(char* date, struct tm* tm)
{
    char* format;
    char* formats[] = {
        "%F %I", /* "2011-03-12 06:38:05 AM" */
        "%F %T", /* "2011-03-12 18:38:05" */
        "%F %R", /* "2011-03-12 18:38" */
        NULL,
    };

    for (format = formats[0]; format; ++ format) {
        if (strptime(date, format, &tm)) {
            return 1;
        }
    }

    return 0;
}

int main(int argc, char** argv)
{
    float diff;

    char* date1;
    char* date2;

    struct tm tm1;
    struct tm tm2;

    time_t time1;
    time_t time2;

    if (argc != 3) {
        fprintf(stderr, "usage: compare-date date1 date2\n");
        exit(1);
    }

    date1 = argv[1];
    date2 = argv[2];

    if (!parse_date(date1, &tm1)) {
        fprintf(stderr, "unsupported date: %s\n", date1);
        exit(1);
    }

    if (!parse_date(date2, &tm1)) {
        fprintf(stderr, "unsupported date: %s\n", date2);
        exit(1);
    }

    time1 = mktime(&tm1);
    time2 = mktime(&tm2);
    diff = difftime(time1, time2);

    printf("%s %c %s\n",
        date1,
        (diff < 0 ? '<' : (diff > 0 ? '>' : '==')),
        date2);

    return 0;
}

回答by Bernd Elkemann

You didn't say in which format you have the date, so I will name two common examples:

你没有说日期的格式,所以我举两个常见的例子:

  • If you are using GNU lib-c (or MinGW) and query the time with:

    time_t time (time_t *result)
    
  • 如果您使用 GNU lib-c(或 MinGW)并使用以下命令查询时间:

    time_t time (time_t *result)
    

Then time_tis just a long int, numbers of seconds since epochand you can subtract one date from the other to find out the number of seconds difference.

然后time_t只是long int, 秒数epoch,您可以从另一个日期中减去一个日期以找出秒数差异。

  • If you are using the Windows API and have a filetime-structure:
  • 如果您使用的是 Windows API 并具有文件时间结构:
typedef struct _FILETIME {
    DWORD dwLowDateTime;
    DWORD dwHighDateTime;
} FILETIME, *PFILETIME;

you can cast the pointer to a pointer to ULARGE_INTEGER, and subtract that one giving you the number of 100-nanosecond intervals difference.

您可以将指针转换为指向 的指针ULARGE_INTEGER,然后减去该指针,得到 100 纳秒间隔差异的数量。