C++ 获取当前时间的年,月,日,以及时分秒
生活随笔
收集整理的這篇文章主要介紹了
C++ 获取当前时间的年,月,日,以及时分秒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++ 標準庫沒有提供所謂的日期類型,所以需要引入庫ctime,即在頂部引入#include?<ctime>
獲取年月日,等tm 提供如下
struct tm {int tm_sec; // 秒,正常范圍從 0 到 59 int tm_min; // 分,范圍從 0 到 59int tm_hour; // 小時,范圍從 0 到 23int tm_mday; // 一月中的第幾天,范圍從 1 到 31int tm_mon; // 月,范圍從 0 到 11 ,所以使用的時候+1int tm_year; // 自 1900 年起的年數,所以使用的使用+1900int tm_wday; // 一周中的第幾天,范圍從 0 到 6,從星期日算起int tm_yday; // 一年中的第幾天,范圍從 0 到 365,從 1 月 1 日算起int tm_isdst; // 夏令時
};
具體的代碼如下:
#include <iostream>
using namespace std;
#include <ctime>
int main()
{time_t curtime;time(&curtime);cout << "1970到目前經過秒數:" << time(&curtime) << endl;cout << "本地日期和時間:" << ctime(&curtime) << endl;tm *nowtime = localtime(&curtime);// 輸出tm結構的年月日cout << "年: " << 1900 + nowtime->tm_year << endl;cout << "月: " << 1 + nowtime->tm_mon << endl;cout << "日: " << nowtime->tm_mday << endl;cout << "時間: " << nowtime->tm_hour << ":";cout << nowtime->tm_min << ":";cout << nowtime->tm_sec << endl;return 0;
}
打印結果:
總結
以上是生活随笔為你收集整理的C++ 获取当前时间的年,月,日,以及时分秒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 油多少钱一斤啊?
- 下一篇: erp系统多少钱啊?