Date comparison to find which is bigger in C

前端 未结 4 1271
说谎
说谎 2020-12-19 03:48

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

kindly help me out plz....

Thanks

相关标签:
4条回答
  • 2020-12-19 04:08

    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)
      

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

    • If you are using the Windows API and have a filetime-structure:
    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.

    0 讨论(0)
  • 2020-12-19 04:22

    You can use the difftime function:

    #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 mktime to convert them.

    0 讨论(0)
  • 2020-12-19 04:24
    #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;
        }
    }
    
    0 讨论(0)
  • 2020-12-19 04:24

    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 strptime to convert the string to a struct tm, and then do the comparison.

    #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;
    }
    
    0 讨论(0)
提交回复
热议问题