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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:38:05  来源:igfitidea点击:

Example of tm use

ctm

提问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结构

  1. call time()to get current date/time as number of seconds since 1 Jan 1970.
  2. call localtime()to get struct tmpointer. If you want GMT them call gmtime()instead of localtime().

  3. Use sprintf()or strftime()to convert the struct tm to a string in any format you want.

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

  3. 使用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

References:

参考: