跳到主要内容

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_wdayMon 到 Sun
Mmm月份的 3 个字母缩写time_ptr->tm_monJan 到 Dec
dd月份中的天数,2 位数字time_ptr->tm_mday00 到 31
hh小时,2 位数字time_ptr->tm_hour00 到 23
mm分钟,2 位数字time_ptr->tm_min00 到 59
ss秒数,2 位数字time_ptr->tm_sec00 到 59
yyyy年份,4 位数字time_ptr->tm_year + 19004 位数字

asctime() 参数

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

asctime() 返回值

  • 指向给定时间的字符表示的空终止字符串的指针。

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

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

int main()
{
time_t curr_time;

time(&curr_time);
cout << "当前日期和时间: " << asctime(localtime(&curr_time));

return 0;
}

当你运行程序时,输出将会是:

当前日期和时间: Tue Mar 21 13:52:57 2017