8

你能举一个使用tm(我不知道如何初始化它struct)的例子,当前日期是用这种格式写的y/m/d吗?

4

1 回答 1

11

如何使用tm结构

  1. 调用time()以获取当前日期/时间作为自 1970 年 1 月 1 日以来的秒数。
  2. 调用localtime()以获取struct tm指针。如果你想要 GMT 他们打电话gmtime()而不是localtime().

  3. 使用sprintf()strftime()将 struct tm 转换为您想要的任何格式的字符串。

例子

#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;
}

示例输出

Now it's 12/10/24

参考:

于 2012-12-01T11:20:09.677 回答