Qt中的日期和时间

这篇具有很好参考价值的文章主要介绍了Qt中的日期和时间。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

QDate

示例(打印年月日):

 QTime

示例(显示时分秒):

QDateTime

示例(显示当前日期和时间):

示例(分别取出 年 月 日 时 分 秒):

QDate

        

        QDate是Qt库中的日期类,提供了一种方便的方式来处理日期。它主要用于处理日期和时间相关的操作,包括日期的计算、格式化、比较和转换等。QDate可以处理的日期范围是从公元前4713年1月1日至公元7999年12月31日。

QDate的使用场景包括:  

1. 计算日期:QDate可以用于计算两个日期之间的天数、月数、年数,以及判断某一天是星期几等。 

2. 格式化日期:QDate可以将日期转换为不同的字符串格式,如"yyyy-MM-dd"、"dd.MM.yyyy"、"ddd MMM d yyyy"等。 

3. 比较日期:QDate提供了比较操作符,可以方便地比较两个日期的大小。 

4. 日期转换:QDate可以将日期转换为Unix时间戳或Julian日期等不同的日期格式。 

5. 绘制日期:QDate可以用于绘制日历等日期相关的界面。 

        总之,QDate是一个非常实用的日期处理类,在Qt开发中经常被使用。

// 构造函数
QDate::QDate();
QDate::QDate(int y, int m, int d);

// 公共成员函数
// 重新设置日期对象中的日期
bool QDate::setDate(int year, int month, int day);
// 给日期对象添加 ndays 天
QDate QDate::addDays(qint64 ndays) const;
// 给日期对象添加 nmonths 月
QDate QDate::addMonths(int nmonths) const;
// 给日期对象添加 nyears 月
QDate QDate::addYears(int nyears) const;

// 得到日期对象中的年/月/日
int QDate::year() const;
int QDate::month() const;
int QDate::day() const;
void QDate::getDate(int *year, int *month, int *day) const;

// 日期对象格式化
/*
    d    - The day as a number without a leading zero (1 to 31)
    dd   - The day as a number with a leading zero (01 to 31)
    ddd	 - The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses the system locale to localize the name, i.e. QLocale::system().
    dddd - The long localized day name (e.g. 'Monday' to 'Sunday'). Uses the system locale to localize the name, i.e. QLocale::system().
    M    - The month as a number without a leading zero (1 to 12)
    MM   - The month as a number with a leading zero (01 to 12)
    MMM	 - The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses the system locale to localize the name, i.e. QLocale::system().
    MMMM - The long localized month name (e.g. 'January' to 'December'). Uses the system locale to localize the name, i.e. QLocale::system().
    yy   - The year as a two digit number (00 to 99)
    yyyy - The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.
*/
QString QDate::toString(const QString &format) const;

// 操作符重载 ==> 日期比较
bool QDate::operator!=(const QDate &d) const;
bool QDate::operator<(const QDate &d) const;
bool QDate::operator<=(const QDate &d) const;
bool QDate::operator==(const QDate &d) const;
bool QDate::operator>(const QDate &d) const;
bool QDate::operator>=(const QDate &d) const;

// 静态函数 -> 得到本地的当前日期
[static] QDate QDate::currentDate();
示例(打印年月日):

qt 时间显示,# QT,qt,开发语言

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //获取当前日期
    QDate d =QDate::currentDate();
    //第一种方式
    qDebug()<<"year:" <<d.year()<<", month: "<<d.month()<<",day: "<< d.day();
    //第二种方式 2000-01-01
    QString str = d.toString("yyyy-MM-dd");
    qDebug()<<"date str: "<<str;



}

MainWindow::~MainWindow()
{
    delete ui;
}

 运行结果:

qt 时间显示,# QT,qt,开发语言

 QTime

  QTime是Qt库中的时间类,提供了一种方便的方式来处理时间。它主要用于处理时间相关的操作,包括时间的计算、格式化、比较和转换等。QTime可以处理的时间范围是从0:0:0.000至23:59:59.999。

QTime的使用场景包括:

1. 计算时间:QTime可以用于计算两个时间之间的差值,包括小时数、分钟数、秒数和毫秒数。

2. 格式化时间:QTime可以将时间转换为不同的字符串格式,如"H:mm:ss.zzz"、"hh:mm AP"等。

3. 比较时间:QTime提供了比较操作符,可以方便地比较两个时间的大小。

4. 时间转换:QTime可以将时间转换为Unix时间戳或以毫秒为单位的时间等不同的时间格式。

5. 绘制时间:QTime可以用于绘制钟表等时间相关的界面。

  总之,QTime是一个非常实用的时间处理类,在Qt开发中经常被使用。

// 构造函数
QTime::QTime();
/*
    h 		==> 取值范围: 0 ~ 23
    m and s 	==> 取值范围: 0 ~ 59
    ms 		==> 取值范围: 0 ~ 999
*/ 
QTime::QTime(int h, int m, int s = 0, int ms = 0);

// 公共成员函数
// Returns true if the set time is valid; otherwise returns false.
bool QTime::setHMS(int h, int m, int s, int ms = 0);
QTime QTime::addSecs(int s) const;
QTime QTime::addMSecs(int ms) const;

// 示例代码
  QTime n(14, 0, 0);                // n == 14:00:00
  QTime t;
  t = n.addSecs(70);                // t == 14:01:10
  t = n.addSecs(-70);               // t == 13:58:50
  t = n.addSecs(10 * 60 * 60 + 5);  // t == 00:00:05
  t = n.addSecs(-15 * 60 * 60);     // t == 23:00:00

// 从时间对象中取出 时/分/秒/毫秒
// Returns the hour part (0 to 23) of the time. Returns -1 if the time is invalid.
int QTime::hour() const;
// Returns the minute part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::minute() const;
// Returns the second part (0 to 59) of the time. Returns -1 if the time is invalid.
int QTime::second() const;
// Returns the millisecond part (0 to 999) of the time. Returns -1 if the time is invalid.
int QTime::msec() const;


// 时间格式化
/*
    -- 时 --
    h	==>	The hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
    hh	==>	The hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
    H	==>	The hour without a leading zero (0 to 23, even with AM/PM display)
    HH	==>	The hour with a leading zero (00 to 23, even with AM/PM display)
    -- 分 --
    m	==>	The minute without a leading zero (0 to 59)
    mm	==>	The minute with a leading zero (00 to 59)
    -- 秒 --
    s	==>	The whole second, without any leading zero (0 to 59)
    ss	==>	The whole second, with a leading zero where applicable (00 to 59)
    -- 毫秒 --
    zzz	==>	The fractional part of the second, to millisecond precision, 
			including trailing zeroes where applicable (000 to 999).
    -- 上午或者下午
    AP or A	==>	使用AM/PM(大写) 描述上下午, 中文系统显示汉字
    ap or a	==>	使用am/pm(小写) 描述上下午, 中文系统显示汉字
*/
QString QTime::toString(const QString &format) const;

// 阶段性计时
// 过时的API函数
// 开始计时
void QTime::start();
// 计时结束
int QTime::elapsed() const;
// 重新计时
int QTime::restart();

// 推荐使用的API函数
// QElapsedTimer 类
void QElapsedTimer::start();
qint64 QElapsedTimer::restart();
qint64 QElapsedTimer::elapsed() const;


// 操作符重载 ==> 时间比较
bool QTime::operator!=(const QTime &t) const;
bool QTime::operator<(const QTime &t) const;
bool QTime::operator<=(const QTime &t) const;
bool QTime::operator==(const QTime &t) const;
bool QTime::operator>(const QTime &t) const;
bool QTime::operator>=(const QTime &t) const;

// 静态函数 -> 得到当前时间
[static] QTime QTime::currentTime();
示例(显示时分秒):

qt 时间显示,# QT,qt,开发语言

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //获取当前时间
    QTime curtime = QTime::currentTime();
    //方式1
    qDebug()<<"hour: "<<curtime.hour() <<",minute: "<<curtime.minute()
             <<", second: "<<curtime.second()<<" , millisecond: "<<curtime.msec();
    //方式2
    QString strm = curtime.toString("hh:mm:ss.zzz");
    qDebug()<<"格式化的日期: "<<strm;
}

MainWindow::~MainWindow()
{
    delete ui;
}

 运行结果:

qt 时间显示,# QT,qt,开发语言

QDateTime

  QDateTime是Qt库中的日期时间类,它可以同时处理日期和时间信息。QDateTime提供了一系列的函数来处理日期和时间的格式化、比较、计算和转换等操作。

QDateTime的使用场景包括:

1. 使计算时间:QDateTime可以用于计算两个日期之间的差值、计算某个日期之前或之后的几天、几个月或几年等。

2. 格式化时间:QDateTime可以将日期时间转换为不同的字符串格式,如"yyyy-MM-dd", "hh:mm:ss zzz"等。

3. 比较时间:QDateTime提供了比较操作符,可以方便地比较两个日期时间的大小,判断哪一个更早或更晚。

4. 时间转换:QDateTime可以将日期时间转换为Unix时间戳或以毫秒为单位的时间等不同的时间格式。

5. 绘制时间:QDateTime可以用于绘制日历等日期时间相关的界面。

  总之,QDateTime是一个非常实用的日期时间处理类,在Qt开发中经常被使用。无论是处理时间戳,构建日历,或是格式日期时间,QDateTime都是一个非常实用的工具。

// 构造函数
QDateTime::QDateTime();
QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);

// 公共成员函数
// 设置日期
void QDateTime::setDate(const QDate &date);
// 设置时间
void QDateTime::setTime(const QTime &time);
// 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
QDateTime QDateTime::addYears(int nyears) const;
QDateTime QDateTime::addMonths(int nmonths) const;
QDateTime QDateTime::addDays(qint64 ndays) const;
QDateTime QDateTime::addSecs(qint64 s) const;
QDateTime QDateTime::addMSecs(qint64 msecs) const;

// 得到对象中的日期
QDate QDateTime::date() const;
// 得到对象中的时间
QTime QDateTime::time() const;

// 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
QString QDateTime::toString(const QString &format) const;


// 操作符重载 ==> 日期时间对象的比较
bool QDateTime::operator!=(const QDateTime &other) const;
bool QDateTime::operator<(const QDateTime &other) const;
bool QDateTime::operator<=(const QDateTime &other) const;
bool QDateTime::operator==(const QDateTime &other) const;
bool QDateTime::operator>(const QDateTime &other) const;
bool QDateTime::operator>=(const QDateTime &other) const;

// 静态函数
// 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
[static] QDateTime QDateTime::currentDateTime();
示例(显示当前日期和时间):

qt 时间显示,# QT,qt,开发语言

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //获取当前的日期和时间
    QDateTime dt =QDateTime::currentDateTime();
    //格式化 yyyy//MM/dd hh:mm:ss ap
    QString strdt = dt.toString("yyyy//MM/dd hh:mm:ss ap");
    QString strdt1 = dt.toString("yyyy//MM/dd hh:mm:ss ");
    QString strdt2 = dt.toString("yyyy//MM/dd HH:mm:ss ap");
    qDebug()<<"当前的日期和时间:"<<strdt;
    qDebug()<<"当前的日期和时间:"<<strdt1;
    qDebug()<<"当前的日期和时间:"<<strdt2;


}

MainWindow::~MainWindow()
{
    delete ui;
}

 运行结果:

qt 时间显示,# QT,qt,开发语言

 可以看到当我们采用的格式不同时,得到的日期时间显示结果也不同

示例(分别取出 年 月 日 时 分 秒):

qt 时间显示,# QT,qt,开发语言

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QTime>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QDateTime dt =QDateTime::currentDateTime();
    //先取出日期
    QDate d = dt.date();
    qDebug()<<"year: "<<d.year()<<", month:"<<d.month()<<" , day:"<<d.day();
    //再取出时间
    QTime t = dt.time();
    qDebug()<<"hour: "<<t.hour() <<",minute: "<<t.minute()
            <<", second: "<<t.second()<<" , millisecond: "<<t.msec();

}

MainWindow::~MainWindow()
{
    delete ui;
}

运行结果:

qt 时间显示,# QT,qt,开发语言

总结:本文讲解了Qt中会使用到的时间和日期的相关知识 文章来源地址https://www.toymoban.com/news/detail-701494.html

到了这里,关于Qt中的日期和时间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Qt:日期与时间戳互相转换Demo

    1、dialog.h 2、dialog.cpp 3、运行界面:  

    2024年02月14日
    浏览(31)
  • 【QT】如何实时显示当前时间在UI上

    2024年02月15日
    浏览(23)
  • Qt学习1:ui界面显示实时时间(自己学习用)

    Qt获取系统当前时间,ui界面显示实时时间和日期。 一、ui放入Qlabel 开关按钮是控制灯的,不用管。 1. 静态时间显示用虚拟text 二、.cpp添加代码 槽是 C++ 成员函数,可以被调用。 private slots:在这个区内声明的槽意味着只有类自己可以将信号与之相连接。(一对一关系) 在

    2024年02月08日
    浏览(35)
  • C++ Qt框架开发 | 基于Qt框架开发实时成绩显示排序系统(3) 保存表格数据

    对上两篇篇的工作C++ Qt框架开发| 基于Qt框架开发实时成绩显示排序系统(1)-CSDN博客和C++ Qt框架开发 | 基于Qt框架开发实时成绩显示排序系统(2)折线图显示-CSDN博客继续优化,增加一个 保存按钮,用于保存成绩数据。 1)在ui界面添加一个按钮         将其命名为saveBtn。

    2024年02月19日
    浏览(29)
  • 让QT中的Qtablewidget控件表格中的元素内容显示居中

    好了看最终的一个演示出来的效果吧  好了,就到这里,下次其他的我再继续更新吧,下面我附上一个类似的文章,写的也可以,有些我没有用到的分享出来你们也可以借鉴一下。 Qt中让tableWidget内容中的每个元素居中(qtablewidget的一些使用) - ww学习笔记 - 博客园

    2024年02月11日
    浏览(36)
  • QT支持多种开发语言

    QT主要是一个C++应用程序框架,但它也提供了对其他一些编程语言的官方或非官方支持。以下是QT支持的一些语言版本及其特点。北京木奇移动技术有限公司,专业的软件外包开发公司,欢迎交流合作。 1.Python (PyQt)  : PyQt是QT的官方Python绑定,允许Python开发者使用QT库来创建

    2024年04月29日
    浏览(36)
  • 【QT开发(5)】0919-QT里面新增ui类,新增使用opencv读取图片的普通类,在ui类中显示图片

    1、Qt Creator快速入门_第三版__霍亚飞编著 2、《Qt+OpenCV显示图片(Mat转QImage然后显示在QLabel上)》 https://gitee.com/hiyanyx/qt5.14-cpp_-empty_-project/tree/Study2023-section5/ git分支“Study2023-section5” 新增ui类 新增使用opencv读取图片的普通类 为了更加方便,可在QT 中添加普通类,这样会自动生

    2024年02月07日
    浏览(33)
  • Qt应用开发(基础篇)——时间微调输入框QDateTimeEdit、QDateEdit、QTimeEdit

             QAbstractSpinBox 是全部微调输入框的父类,这是一种允许用户通过点击上下箭头按钮或输入数字来调整数值的图形用户界面控件,父类提供了当前值 text 、对齐方式 align 、只读 readOnly 等通用属性和方法。在上一篇数值微调输入框中有详细介绍。          QDate

    2024年02月14日
    浏览(35)
  • 【Visual Studio】Qt 的实时绘图曲线功能,使用 C++ 语言,配合 Qt 开发串口通信界面

    知识不是单独的,一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏:Visual Studio。 战斗背景:做了个串口接收界面,用来接收传输过来的信号。但是光用数字显示太单调,需要用图线显示出来。 战略目标:干掉它。 战术路线:Qt 绘图可以使用 Qt Charts,先了解

    2024年02月11日
    浏览(38)
  • 【[Qt]基于QChartView开发的图表显示控件,支持实时显示,动态更新,支持鼠标交互等操作】

    十字线和显示坐标实现 在.h文件中定义十字线lineitem变量和坐标textitem变量 在Cpp文件中初始化 然后定义鼠标事件,在鼠标进入时显示,移出时隐藏,移动时显示。 其他实现请参考具体代码 ChartDrawer.h文件 ChartDrawer.cpp 文件 具体使用代码如下 1、初始化类对象,并加入界面布局

    2023年04月23日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包