Qt中的日期和时间

本文详细介绍了Qt库中的QDate、QTime和QDateTime类,它们分别用于处理日期、时间及日期时间。这些类提供了构造函数、成员函数以及操作符重载,支持日期时间的计算、格式化、比较和转换。通过示例代码展示了如何获取和打印当前日期、时间以及如何进行日期时间的格式化输出。QDate和QTime可用于计算和格式化日期时间,而QDateTime结合了日期和时间的功能,更加全面。
摘要由CSDN通过智能技术生成

目录

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();
示例(打印年月日):

#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;
}

 运行结果:

 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();
示例(显示时分秒):

#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;
}

 运行结果:

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();
示例(显示当前日期和时间):

#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;
}

 运行结果:

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

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

#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中会使用到的时间和日期的相关知识 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小梁今天敲代码了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值