如何用 C++ 获取当前时间和日期?
下面是 C++ 语言中获取当前日期和时间的一个示例:
示例
#include <iostream>
using namespace std;
int main() {
time_t now = time(0);
char *date = ctime(& now);
cout << "The local date and time : " << date << endl;
}输出
以下是输出
The local date and time : Wed Oct 3 08:35:24 2018
在上面的程序中,获取当前日期和时间的代码位于 main() 中。此处,time_t 是变量 now 的返回类型。内置函数 time() 用于获取本地当前时间和日期。
time_t now = time(0); char *date = ctime(& now);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP