第2个回答 2005-10-17
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define START_YEAR 1900
void printdate(tm* ltime);
main()
{
tm *mytime;
time_t buf;
buf=time(0);
mytime=(tm*) malloc(sizeof(tm));
mytime=localtime(&buf);
printdate(mytime);
return 0;
}
void printdate(tm* ltime)
{
printf("%d/%d/%d",ltime->tm_mday,ltime->tm_mon+1,ltime->tm_year+START_YEAR);
} //由于月份是0--11,所以转换成实际要+1;年份是从1900年开始算过去了多少年
P.S
tm是一个struct,具体定义如下:
struct tm
{
int tm_sec;// Seconds: 0-59
int tm_min;// Minutes: 0-59
int tm_hour;// Hours since midnight: 0-23
int tm_mday;// Day of the month: 1-31
int tm_mon;// Months *since* January: 0-11
int tm_year;// Years since 1900
int tm_wday;// Days since Sunday (0-6)
int tm_yday;// Days since Jan. 1: 0-365
int tm_isdst;// +1 Daylight Savings Time, 0 No DST
};
结构有了,要和任何一个日期对比就很简单了,只要提取出具体的tm_mon,tm_year,tm_mday就可以对比了。本回答被提问者采纳