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

更新時間:2020 年 6 月 25 日

842 次瀏覽

開啟你的職業生涯

完成課程後即可獲得認證

開始吧
廣告