回答
你沒「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 - 納秒間隔差。
time_t在Windows編譯器上同樣有效。你的回答讓我覺得它在Visual C++編譯器中是不可用的...... – rubenvb 2011-03-12 15:10:39
@rubenvb是的。這些只是例子。 – 2011-03-12 15:37:23
'time_t'是標準C89中的一種類型,因此由MSVC和Unix C編譯器支持。雖然有許多不同的亞秒時間結構。確實是 – 2011-03-12 15:37:36
#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;
}
}
'if(d1.year = d2.year)'應該有== ==我想:-) – 2011-03-12 15:02:07
@ eznme:謝謝:) – SharpUrBrain 2011-03-12 15:05:36
你可以提供更多關於你想達到的信息嗎?因爲比較日期非常簡單。畢竟,自從給定的過去日期或包含年,月,日,...的結構之後,它們只是秒數(或毫,微,納,......)。無論格式如何,比較都應該很容易去表演。
也許你想比較用戶給出的兩個日期字符串(類似「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;
}
- 1. c#,比較日期
- 2. 在C中比較日期#
- 3. 在C++中比較日期
- 4. c#比較日期時間和其他2日期
- 5. 比較日期時出錯
- 6. PostgreSQL和C++ - 比較日期
- 7. ASP.NET C#日期比較
- 8. iphone,objective c - 日期比較
- 9. Objective-C比較2日期
- 10. 如何比較日期c#
- 11. C#比較日期範圍
- 12. 誤差比較日期找出MAX(日)在MySQL
- 13. 比較日期
- 14. 日期比較
- 15. 比較日期
- 16. 日期比較
- 17. 日期比較
- 18. 日期比較
- 19. 日期比較
- 20. 比較日期
- 21. 比較日期
- 22. 日期比較
- 23. 日期比較
- 24. 比較日期
- 25. 比較在C++ 2個日期
- 26. 如何在linq c中比較日期#
- 27. 比較兩個日期在objective-c
- 28. 如何比較兩個日期在c#
- 29. 比較兩個日期在C#
- 30. 在DataView.RowFilter中比較日期?
日期如何提供給您? – 2011-03-12 14:37:02
比較'sizeof(date1)> sizeof(date2)'告訴你哪個更大,或者可能將日期與我的結婚紀念日進行比較,這對我來說是個大日子?早/晚是比較日期的常用方式,因爲大小取決於您計算的位置,並不一定有意義。 – 2011-03-12 15:21:43