C++ asctime()

asctime() 函数定义在 <ctime> 头文件中。

asctime() 原型

char* asctime(const struct tm * time_ptr);

asctime() 函数接收一个指向 tm 对象的 指针 作为参数,并返回给定日历时间的文本表示,格式如下:

Www Mmm dd hh:mm:ss yyyy

asctime() 表示

类型 描述 从何获得
Www 3 字母星期名称 time_ptr -> tm_wday 周一至周日
Mmm 3 字母月份名称 time_ptr -> tm_mon 一月至十二月
dd 2 位数日期 time_ptr -> tm_mday 00 至 31
hh 2 位数小时 time_ptr -> tm_hour 00 至 23
mm 2 位数分钟 time_ptr -> tm_min 00 至 59
ss 2 位数秒 time_ptr -> tm_sec 00 至 59
yyyy 4 位数年份 time_ptr -> tm_year + 1900 4 位数年份

asctime() 参数

  • time_ptr: 指向待转换的 tm 对象的指针。

asctime() 返回值

  • 指向以 null 结尾的字符串的指针,该字符串指向给定时间的字符表示。

示例:asctime() 函数如何工作?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
	time_t curr_time;

	time(&curr_time);
	cout << "Current date and time: " << asctime(localtime(&curr_time));

	return 0;
}

运行程序后,输出将是

Current date and time: Tue Mar 21 13:52:57 2017

另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战