2011-03-12 58 views
9

更大,我想知道如何找到它是用C語言程序日期比較,找出其在C

好心幫我PLZ更大的日期....

感謝

+5

日期如何提供給您? – 2011-03-12 14:37:02

+5

比較'sizeof(date1)> sizeof(date2)'告訴你哪個更大,或者可能將日期與我的結婚紀念日進行比較,這對我來說是個大日子?早/晚是比較日期的常用方式,因爲大小取決於您計算的位置,並不一定有意義。 – 2011-03-12 15:21:43

回答

0

你沒「T說以哪種方式你有約會,所以我會名稱的兩個常見的例子:如果你使用GNU LIB-C(或MinGW的)

  • 與查詢時間:

     
    time_t time (time_t *result) 
    

然後time_t只是long int,幾秒鐘數量,因爲epoch,你可以減去另一個日期,找出差異秒的數量。

  • 如果您使用的是Windows API,並有一個FILETIME結構:
 
typedef struct _FILETIME { 
    DWORD dwLowDateTime; 
    DWORD dwHighDateTime; 
} FILETIME, *PFILETIME; 

,就可以把指針的指針ULARGE_INTEGER,並減去一個給你一個數100 - 納秒間隔差。

+0

time_t在Windows編譯器上同樣有​​效。你的回答讓我覺得它在Visual C++編譯器中是不可用的...... – rubenvb 2011-03-12 15:10:39

+0

@rubenvb是的。這些只是例子。 – 2011-03-12 15:37:23

+0

'time_t'是標準C89中的一種類型,因此由MSVC和Unix C編譯器支持。雖然有許多不同的亞秒時間結構。確實是 – 2011-03-12 15:37:36

12

可以使用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; 
} 

如果您的日期是time_t型的沒有,你可以使用函數mktime將它們轉換。

4
#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

'if(d1.year = d2.year)'應該有== ==我想:-) – 2011-03-12 15:02:07

+0

@ eznme:謝謝:) – SharpUrBrain 2011-03-12 15:05:36

0

你可以提供更多關於你想達到的信息嗎?因爲比較日期非常簡單。畢竟,自從給定的過去日期或包含年,月,日,...的結構之後,它們只是秒數(或毫,微,納,......)。無論格式如何,比較都應該很容易去表演。

也許你想比較用戶給出的兩個日期字符串(類似「2011-03-12 18:38」)?然後,您可以使用strptime將該字符串轉換爲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; 
}