C语⾔中将⽇期和时间以字符串格式输出的⽅法
ctime()函数:
头⽂件:
#include <time.h>
定义函数:
char *ctime(const time_t *timep);
函数说明:ctime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使⽤的时间⽇期表⽰⽅法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993\n"。
注意:若再调⽤相关的时间⽇期函数,此字符串可能会被破坏。
返回值:返回⼀字符串表⽰⽬前当地的时间⽇期。
范例
#include <time.h>
main(){
time_t timep;
time (&timep);
printf("%s", ctime(&timep));
}
执⾏
Sat Oct 28 10 : 12 : 05 2000
asctime()函数:
头⽂件:
#include <time.h>
定义函数:
char *asctime(const struct tm * timeptr);
函数说明:asctime()将参数timeptr 所指的tm 结构中的信息转换成真实世界所使⽤的时间⽇期表⽰⽅法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:"Wed Jun 30 21:49:08 1993\n"
返回值:若再调⽤相关的时间⽇期函数,此字符串可能会被破坏。此函数与ctime 不同处在于传⼊的参数是不同的结构。
附加说明:返回⼀字符串表⽰⽬前当地的时间⽇期.
范例
日期转字符串函数
#include <time.h>
main(){
time_t timep;
time (&timep);
printf("%s", asctime(gmtime(&timep)));
}
执⾏
Sat Oct 28 02:10:06 2000

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。