C语言 tm 使用示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13658756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of tm use
提问by tomss
Can you give an example of use of tm(I don't know how to initialize that struct) where the current date is written in this format y/m/d?
您能否举一个使用tm(我不知道如何初始化struct)的示例,其中当前日期以这种格式写入y/m/d?
回答by Jomoos
How to use tmstructure
如何使用tm结构
- call
time()to get current date/time as number of seconds since 1 Jan 1970. call
localtime()to getstruct tmpointer. If you want GMT them callgmtime()instead oflocaltime().Use
sprintf()orstrftime()to convert the struct tm to a string in any format you want.
- 调用
time()以获取自 1970 年 1 月 1 日以来的秒数形式的当前日期/时间。 调用
localtime()获取struct tm指针。如果你想要格林威治标准时间他们打电话gmtime()而不是localtime().使用
sprintf()或strftime()将 struct tm 转换为您想要的任何格式的字符串。
Example
例子
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"Now it's %y/%m/%d.",timeinfo);
puts (buffer);
return 0;
}
Example Output
示例输出
Now it's 12/10/24

