原型: char *asctime(const struct tm *tblock)
功能:将struct tm结构类型时间日期转换为ASCII码
参数:
timeptr 是指向 tm 结构的指针,包含了分解为如下各部分的日历时间:
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59*/
int tm_hour; /* 小时,范围从 0 到 23*/
int tm_mday; /* 一月中的第几天,范围从 1 到 31*/
int tm_mon; /* 月份,范围从 0 到 11 */
int tm_year; /* 自 1900 起的年数*/
int tm_wday; /* 一周中的第几天,范围从 0 到 6*/
int tm_yday; /* 一年中的第几天,范围从 0 到 365*/
int tm_isdst; /* 夏令时 */
};
返回值:该函数返回一个C字符串,包含了可读格式的日期和时间信息 DDD MMM dd hh:mm:ss YYYY,其中,Www表示星期几,Mmm是以字母表示的月份,d表示一月中的第几天,hh:mm:ss 表示时间,yyyy表示年份。
#include /*printf*/
#include /*strcpy*/
#include /*asctime*/
int main()
{
struct tm t;
/* sample loading of tm structure */
t.tm_sec = 5; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 8; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 7; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 1; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated string */
char str[80];
strcpy(str, asctime(&t));
printf("时间: %s\n", str);
return 0;
}
结果
时间: Mon Aug 22 08:30:05 1956