呼叫當前年月日

會武術之白貓發表於2018-11-05

直接顯示當前時間:

1 #include <iostream>
2 #include <ctime>
3 using namespace std;
4 int main()
5 {
6     time_t now = time(0);
7     char *dt = ctime(&now);
8     cout << dt;
9 }

顯示當前的年月日,時分秒:

 1 #include <iostream>
 2 #include <ctime>
 3 using namespace std;
 4 int main()
 5 {
 6     time_t now = time(0);
 7     tm *ltm = localtime(&now);
 8     cout << "" << 1900 + ltm->tm_year << endl;
 9     cout << "" << 1 + ltm->tm_mon << endl;
10     cout << "" << ltm->tm_mday << endl;
11     cout << "" << ltm->tm_hour << endl;
12     cout << "" << ltm->tm_min << endl;
13     cout << "" << ltm->tm_sec << endl;
14 }

 

相關文章