欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT獲取顯示當(dāng)前時(shí)間和日期的方法(用QTime,QDate和QDateTime)

 更新時(shí)間:2022年08月11日 10:39:40   作者:YJer  
獲取當(dāng)期日期時(shí)間在我們?nèi)粘i_發(fā)中經(jīng)常會遇到,下面這篇文章主要給大家介紹了關(guān)于QT獲取顯示當(dāng)前時(shí)間和日期的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

獲取當(dāng)前時(shí)間和日期

QT中獲取時(shí)間和日期的主要是 QTime 、QDate 和 QDateTime 這三個類。

QTime 類

通過 QTime 類中提供的時(shí)間相關(guān)的方法,可以獲取到當(dāng)前系統(tǒng)時(shí)間(時(shí)、分、秒、毫秒),需要注意的是,計(jì)時(shí)的準(zhǔn)確性由底層操作系統(tǒng)決定,并不是所有的操作系統(tǒng)都能精確到毫秒級別。

通過調(diào)用 QTime 類中的 currentTime() 方法可以獲取到當(dāng)前系統(tǒng)時(shí)間:

QTime time = QTime::currentTime();
qDebug() << time;

輸出結(jié)果:
QTime("12:01:13.427")

如果我們需要獲取字符串形式的時(shí)間,可以使用 toString() 這個方法:

QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss");

輸出結(jié)果:
"12:01:13"

字符串形式的時(shí)間輸出格式由 toString() 方法中的 format 參數(shù)列表決定,可用的參數(shù)列表如下:

如果我們在顯示時(shí)間的同時(shí)還需要顯示上午或下午,可以在格式列表添加添加 “AP、A、ap、a” 等選項(xiàng):

QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss a");

輸出結(jié)果:
"02:29:31 下午"

當(dāng)你電腦的系統(tǒng)語言使用中文時(shí),不管格式列表中填 AP、A、ap、a 這四個選項(xiàng)里的哪一個,都只會顯示上午或下午;只有當(dāng)電腦系統(tǒng)語言使用英文時(shí)才會區(qū)分大小寫,例如選擇 AP/A,顯示 AM/PM,選擇 ap/a,顯示 am/pm 。

hh字段的顯示格式受 AP/A 或 ap/a 影響,如果格式列表中使用了 AP/A 或 ap/a 選項(xiàng)區(qū)分上下午,則 hh字段采用12小時(shí)制格式顯示;否則使用24小時(shí)制格式顯示:

QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss a");
qDebug() << time.toString("hh:mm:ss");

輸出結(jié)果:
"02:50:38 下午"
"14:50:38"

HH字段的顯示格式則不受 AP/A 或 ap/a 影響,不管格式列表中是否使用 AP/A 或 ap/a 選項(xiàng)區(qū)分上下午,HH字段均采用24小時(shí)制格式顯示:

QTime time = QTime::currentTime();
qDebug() << time.toString("HH:mm:ss a");
qDebug() << time.toString("HH:mm:ss");

輸出結(jié)果:
"14:52:03 下午"
"14:52:03"

在格式列表中添加 t 選項(xiàng)可以用來獲取時(shí)區(qū)信息:

QTime time = QTime::currentTime();
qDebug() << time.toString("hh:mm:ss t");

輸出結(jié)果:
"14:59:02 中國標(biāo)準(zhǔn)時(shí)間"
    
修改時(shí)區(qū)后輸出結(jié)果:
"14:00:45 新西伯利亞標(biāo)準(zhǔn)時(shí)間"

QDate類

通過調(diào)用 QDate 類中的 currentDate() 方法可以獲取到當(dāng)前系統(tǒng)日期:

QDate date = QDate::currentDate();
qDebug() << date;
qDebug() << date.toString("yyyy-MM-dd");

輸出結(jié)果:
QDate("2022-04-29")
"2022-04-29"

QDate類中對日期的操作與QTime類中對時(shí)間的操作基本一樣,需要字符串格式的日期時(shí),使用 toString() 方法即可,QDate類中對日期操作常用格式如下:

需要顯示星期時(shí),使用 ddd 或 dddd 選項(xiàng):

QDate date = QDate::currentDate();
qDebug() << date;
qDebug() << date.toString("yyyy-MM-dd ddd");
qDebug() << date.toString("yyyy-MM-dd dddd");

輸出結(jié)果:
"2022-04-29 周五"
"2022-04-29 星期五"

QDateTime類

QDateTime類是 QDate 和 QTime 的組合,提供一系列時(shí)間和日期相關(guān)的函數(shù)。

通過調(diào)用 QDateTime 類中的 currentDateTime() 方法可以獲取到當(dāng)前系統(tǒng)時(shí)間和日期:

QDateTime dateTime;
dateTime = QDateTime::currentDateTime();
qDebug()<<dateTime;
qDebug() << dateTime.toString("yyyy-MM-dd hh:mm:ss ddd");

輸出結(jié)果:
QDateTime(2022-04-29 15:22:23.615 中國標(biāo)準(zhǔn)時(shí)間 Qt::TimeSpec(LocalTime))
"2022-04-29 15:22:23 周五"

使用 toString() 方法將時(shí)間和日期轉(zhuǎn)換成字符串形式時(shí),格式與 QTime、QDate 中的格式一樣。

定時(shí)更新顯示時(shí)間和日期

創(chuàng)建一個定時(shí)器,每秒獲取一次系統(tǒng)時(shí)間和日期,轉(zhuǎn)換成字符串形式后再通過Label空間顯示即可完整代碼如下:

main.cpp

#include "dateTime.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DateTime w;
    w.show();
    return a.exec();
}

dateTime.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDateTime>
#include <QDebug>
#include <QTimer>
#include <QLabel>
#include <QVBoxLayout>
#include <QApplication>

class DateTime : public QWidget
{
    Q_OBJECT

public:
    DateTime(QWidget *parent = nullptr);
    ~DateTime();

    void timeUpdate(void);

private:
    QDateTime dateTime;
    QTimer *timer;
    QLabel *label;
};
#endif

dateTime.cpp

#include "dateTime.h"

DateTime::DateTime(QWidget *parent)
    : QWidget(parent)
{
    //設(shè)置窗口標(biāo)題和窗口大小
    this->setWindowTitle("時(shí)間更新顯示例程");
    this->resize(500, 100);

    //創(chuàng)建label對象顯示時(shí)間和日期
    label = new QLabel(this);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(label);
    this->setLayout(layout);

    //初始化時(shí)間和日期顯示
    dateTime = QDateTime::currentDateTime();
    this->label->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss ddd"));

    //創(chuàng)建定時(shí)器定時(shí)更新時(shí)間和日期
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &DateTime::timeUpdate);
    timer->start(1000);
}

DateTime::~DateTime()
{
    delete timer;
}

void DateTime::timeUpdate(void)
{
    dateTime = QDateTime::currentDateTime();
    this->label->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss ddd"));
}


補(bǔ)充:QT中分別獲取當(dāng)前時(shí)間的年、月、日

百度查了半天,沒找到,就自己寫了一個測試,其實(shí)也很簡單,先用QDate去獲取當(dāng)前的時(shí)間,時(shí)間格式設(shè)置為"yyyy-MM-dd",也就是"年-月-日"的格式,然后再利用字符串切割(strtok函數(shù))去切割成獨(dú)立的年、月、日就OK啦,代碼如下(適合懶人一族,直接復(fù)制粘貼,哈哈^ _ ^)

QDate currentdate = QDate::currentDate();
QString str1 = currentdate.toString("yyyy-MM-dd");
qDebug() << "str1 = " << str1;
QByteArray ba = str1.toLatin1();//將QString 轉(zhuǎn)換為 char *類型
char *dateStr = ba.data();//將QString 轉(zhuǎn)換為 char *類型
char *year = strtok(dateStr,"-");
char *month = strtok(NULL,"-");
char *date = strtok(NULL,"-");
qDebug() << "year is:" << year;
qDebug() << "month is:" << month;
qDebug() << "date is:" << date;

運(yùn)行結(jié)果如下:

總結(jié)

到此這篇關(guān)于QT獲取顯示當(dāng)前時(shí)間和日期的文章就介紹到這了,更多相關(guān)QT獲取顯示當(dāng)前時(shí)間日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論