如何用 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);
廣告