4 回答

TA貢獻1780條經驗 獲得超4個贊
C++ 標準庫沒有提供所謂的日期類型。C++ 繼承了 C 語言用于日期和時間操作的結構和函數。為了使用日期和時間相關的函數和結構,需要在 C++ 程序中引用 <ctime> 頭文件。
有四個與時間相關的類型:clock_t、time_t、size_t 和 tm。類型 clock_t、size_t 和 time_t 能夠把系統時間和日期表示為某種整數。
#include <iostream>
#include <ctime>
using namespace std;
int main( ){
// 基于當前系統的當前日期/時間
time_t now = time(0);
cout << "Number of sec since January 1,1970:" << now << endl;
tm *ltm = localtime(&now);
// 輸出 tm 結構的各個組成部分
cout << "日期/時間: "<< 1900 + ltm->tm_year << "-"<< 1 + ltm->tm_mon<< "-"<< ltm->tm_mday
<< " "<< 1 + ltm->tm_hour << ":"<< 1 + ltm->tm_min << ":"<< 1 + ltm->tm_sec << endl;
}

TA貢獻1816條經驗 獲得超4個贊
#include<ctime>
#include<iostream>
using namespace std;
int main ()
{
time_t t;
time ( &t );
cout<<"\007The current date/time is: "<<ctime(&t);
return 0;
}

TA貢獻1803條經驗 獲得超6個贊
#include<iostream>
#include<ctime>
using namespace std;
void main ()
{
time_t rawtime;
time ( &rawtime );
cout<<"當前時間: "<<ctime(&rawtime)
}
- 4 回答
- 0 關注
- 940 瀏覽
添加回答
舉報