QT获取显示当前时间和日期的方法(用QTime,QDate和QDateTime)

Rhea ·
更新时间:2024-09-20
· 1375 次阅读

目录

获取当前时间和日期

QTime 类

QDate类

QDateTime类

定时更新显示时间和日期

补充:QT中分别获取当前时间的年、月、日

总结

获取当前时间和日期

QT中获取时间和日期的主要是 QTime 、QDate 和 QDateTime 这三个类。

QTime 类

通过 QTime 类中提供的时间相关的方法,可以获取到当前系统时间(时、分、秒、毫秒),需要注意的是,计时的准确性由底层操作系统决定,并不是所有的操作系统都能精确到毫秒级别。

通过调用 QTime 类中的 currentTime() 方法可以获取到当前系统时间:

QTime time = QTime::currentTime(); qDebug() << time; 输出结果: QTime("12:01:13.427")

如果我们需要获取字符串形式的时间,可以使用 toString() 这个方法:

QTime time = QTime::currentTime(); qDebug() << time.toString("hh:mm:ss"); 输出结果: "12:01:13"

字符串形式的时间输出格式由 toString() 方法中的 format 参数列表决定,可用的参数列表如下:

如果我们在显示时间的同时还需要显示上午或下午,可以在格式列表添加添加 “AP、A、ap、a” 等选项:

QTime time = QTime::currentTime(); qDebug() << time.toString("hh:mm:ss a"); 输出结果: "02:29:31 下午"

当你电脑的系统语言使用中文时,不管格式列表中填 AP、A、ap、a 这四个选项里的哪一个,都只会显示上午或下午;只有当电脑系统语言使用英文时才会区分大小写,例如选择 AP/A,显示 AM/PM,选择 ap/a,显示 am/pm 。

hh字段的显示格式受 AP/A 或 ap/a 影响,如果格式列表中使用了 AP/A 或 ap/a 选项区分上下午,则 hh字段采用12小时制格式显示;否则使用24小时制格式显示:

QTime time = QTime::currentTime(); qDebug() << time.toString("hh:mm:ss a"); qDebug() << time.toString("hh:mm:ss"); 输出结果: "02:50:38 下午" "14:50:38"

HH字段的显示格式则不受 AP/A 或 ap/a 影响,不管格式列表中是否使用 AP/A 或 ap/a 选项区分上下午,HH字段均采用24小时制格式显示:

QTime time = QTime::currentTime(); qDebug() << time.toString("HH:mm:ss a"); qDebug() << time.toString("HH:mm:ss"); 输出结果: "14:52:03 下午" "14:52:03"

在格式列表中添加 t 选项可以用来获取时区信息:

QTime time = QTime::currentTime(); qDebug() << time.toString("hh:mm:ss t"); 输出结果: "14:59:02 中国标准时间" 修改时区后输出结果: "14:00:45 新西伯利亚标准时间" QDate类

通过调用 QDate 类中的 currentDate() 方法可以获取到当前系统日期:

QDate date = QDate::currentDate(); qDebug() << date; qDebug() << date.toString("yyyy-MM-dd"); 输出结果: QDate("2022-04-29") "2022-04-29"

QDate类中对日期的操作与QTime类中对时间的操作基本一样,需要字符串格式的日期时,使用 toString() 方法即可,QDate类中对日期操作常用格式如下:

需要显示星期时,使用 ddd 或 dddd 选项:

QDate date = QDate::currentDate(); qDebug() << date; qDebug() << date.toString("yyyy-MM-dd ddd"); qDebug() << date.toString("yyyy-MM-dd dddd"); 输出结果: "2022-04-29 周五" "2022-04-29 星期五" QDateTime类

QDateTime类是 QDate 和 QTime 的组合,提供一系列时间和日期相关的函数。

通过调用 QDateTime 类中的 currentDateTime() 方法可以获取到当前系统时间和日期:

QDateTime dateTime; dateTime = QDateTime::currentDateTime(); qDebug()<<dateTime; qDebug() << dateTime.toString("yyyy-MM-dd hh:mm:ss ddd"); 输出结果: QDateTime(2022-04-29 15:22:23.615 中国标准时间 Qt::TimeSpec(LocalTime)) "2022-04-29 15:22:23 周五"

使用 toString() 方法将时间和日期转换成字符串形式时,格式与 QTime、QDate 中的格式一样。

定时更新显示时间和日期

创建一个定时器,每秒获取一次系统时间和日期,转换成字符串形式后再通过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) { //设置窗口标题和窗口大小 this->setWindowTitle("时间更新显示例程"); this->resize(500, 100); //创建label对象显示时间和日期 label = new QLabel(this); QVBoxLayout *layout = new QVBoxLayout(this); layout->addWidget(label); this->setLayout(layout); //初始化时间和日期显示 dateTime = QDateTime::currentDateTime(); this->label->setText(dateTime.toString("yyyy-MM-dd hh:mm:ss ddd")); //创建定时器定时更新时间和日期 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")); } 补充:QT中分别获取当前时间的年、月、日

百度查了半天,没找到,就自己写了一个测试,其实也很简单,先用QDate去获取当前的时间,时间格式设置为"yyyy-MM-dd",也就是"年-月-日"的格式,然后再利用字符串切割(strtok函数)去切割成独立的年、月、日就OK啦,代码如下(适合懒人一族,直接复制粘贴,哈哈^ _ ^)

QDate currentdate = QDate::currentDate(); QString str1 = currentdate.toString("yyyy-MM-dd"); qDebug() << "str1 = " << str1; QByteArray ba = str1.toLatin1();//将QString 转换为 char *类型 char *dateStr = ba.data();//将QString 转换为 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;

运行结果如下:

总结

到此这篇关于QT获取显示当前时间和日期的文章就介绍到这了,更多相关QT获取显示当前时间日期内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



当前时间 方法

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章