C++|Qt工作笔记-C++获取当前系统时间,Qt获取当前系统时间及各标准间转化
生活随笔
收集整理的這篇文章主要介紹了
C++|Qt工作笔记-C++获取当前系统时间,Qt获取当前系统时间及各标准间转化
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
?
理論
源碼
理論
標(biāo)準(zhǔn)C++中有個(gè)time.h(ctime)的頭文件,他提供了把當(dāng)前時(shí)間轉(zhuǎn)成uint的!
在Qt中有一個(gè)QDateTime的類,這個(gè)類提供了幾個(gè)靜態(tài)方法:
QDateTime::currentDateTime()? ?獲取當(dāng)前時(shí)間
QDateTime::fromTime_t()? ? ?從time_t中獲取當(dāng)前時(shí)間
QDateTime::fromString()? ? ?從String中獲取當(dāng)前時(shí)間
?
?
源碼
C++時(shí)間關(guān)鍵源碼:
#include <time.h>time_t now;cout << (int)time(&now) << endl;?
Qt時(shí)間關(guān)鍵源碼:
#include <QApplication> #include <QDateTime> #include <QDebug> #include <windows.h>int main(int argc, char *argv[]) {QApplication a(argc, argv);uint timeUInt;QString timeStr;while(true){timeUInt = QDateTime::currentDateTime().toTime_t();timeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");qDebug() << "timeUInt:" << timeUInt << "\ttimeStr:" << timeStr;qDebug() << "timeUInt to timeStr:" << QDateTime::fromTime_t(timeUInt).toString("yyyy-MM-dd hh:mm:ss");qDebug() << "timeStr to timeUInt:" << QDateTime::fromString(timeStr ,"yyyy-MM-dd hh:mm:ss").toTime_t();qDebug() << "----------------華麗的分割線----------------";Sleep(1000);}return a.exec(); }運(yùn)行截圖如下:
總結(jié)
以上是生活随笔為你收集整理的C++|Qt工作笔记-C++获取当前系统时间,Qt获取当前系统时间及各标准间转化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Qt文档阅读笔记-Multicast R
- 下一篇: C++ STL list输出和增加