(5)Qt中的日期和时间

简介: 本文介绍了Qt中处理日期和时间的类QDate、QTime和QDateTime,包括它们的格式化选项、构造函数、公共成员函数以及如何进行日期和时间的增减、比较,同时提到了QElapsedTimer作为QTime的替代品用于计时。

QDate

日期对象格式化
d - 没有前导零的日子 (1 to 31)
dd - 前导为0的日子 (01 to 31)
ddd - 显示(缩写) 周一、周二、周三、周四、周五、周六、周日
dddd - 显示(完整) 星期一、星期二、星期三、星期四、星期五、星期六、星期日

M             -    没有前导零的月份(1到12)    
MM          -    前导零的月份(01到12)    
MMM       -    缩写 1月、2月、3月...           
MMMM    -    完整 一月、二月、三月...  

yy            -    两个数字的年 (00 to 99)  
yyyy        -    以四位数表示的年份 
// 构造函数
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;

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();
//举个栗子
QDate date(2022, 10, 21);
qInfo() << date;
//获取年月日
qInfo() << date.year() << date.month() << date.day();
//设置年月日
date.setDate(1900, 10, 1);
qInfo() << date;
//添加时间
date = date.addDays(10);
qInfo() << date;

//将日期格式化成字符串
qInfo() << date.toString();
qInfo() << date.toString(Qt::DateFormat::ISODate);
qInfo() << date.toString("yyyy/MM/dd");

//获取当前系统日期
auto curDate = QDate::currentDate();
qInfo() << curDate;

QTime

时间格式化
-- 时
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(小写) 描述上下午, 中文系统显示汉字 
// 构造函数
QTime::QTime();
/*
    h             ==> must be in the range 0 to 23
    m and s     ==> must be in the range 0 to 59
    ms             ==> must be in the range 0 to 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;

QString QTime::toString(const QString &format) 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();
//举个栗子
QTime time(5, 2, 0);
qInfo() << time;
//获取属性
qInfo() << time.hour() << time.minute() << time.second();
//添加时间
time = time.addSecs(20);
qInfo() << time;
//将时间格式化成字符串
qInfo() << time.toString();
qInfo() << time.toString(Qt::DateFormat::ISODate);
qInfo() << time.toString("hh:mm:ss");
//从指定字符串生成时间
qInfo() << QTime::fromString("05:30:20");
//获取当前时间
QTime curTime = time.currentTime();
qInfo() << curTime;

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();
//举个栗子
QDateTime dt = QDateTime::currentDateTime();
qInfo() << dt;
//获取日期
auto date = dt.date();
auto time = dt.time();
//格式化时间
qInfo() << dt.toString("yyyy年MM月dd日 hh:mm:ss AP");

经时计时器

QTime的经时计时器已经过时了,推荐使用QElapsedTimer。

//QTime已废弃的函数
// 开始计时
void QTime::start();
// 计时结束
int QTime::elapsed() const;
// 重新计时
int QTime::restart();

// 推荐使用的API函数
// QElapsedTimer 类
void QElapsedTimer::start();
qint64 QElapsedTimer::restart();
qint64 QElapsedTimer::elapsed() const;
//举个栗子
//经时计时器
QElapsedTimer eTimer;
eTimer.start();
int count = 0;
for (int i = 0; i < 100000000; i++)
{
    count++;
    //eTimer.restart();  //重新计时
}
qInfo() << "count:" << count << "用时:" << eTimer.elapsed(); //ms

相关文章
|
5月前
【qt】一次性讲清楚日期和时间2
【qt】一次性讲清楚日期和时间
32 0
|
5月前
【qt】一次性讲清楚日期和时间1
【qt】一次性讲清楚日期和时间
63 0
|
3月前
|
数据安全/隐私保护 C++ 计算机视觉
Qt(C++)开发一款图片防盗用水印制作小工具
文本水印是一种常用的防盗用手段,可以将文本信息嵌入到图片、视频等文件中,用于识别和证明文件的版权归属。在数字化和网络化的时代,大量的原创作品容易被不法分子盗用或侵犯版权,因此加入文本水印成为了保护原创作品和维护知识产权的必要手段。 通常情况下,文本水印可以包含版权声明、制作者姓名、日期、网址等信息,以帮助识别文件的来源和版权归属。同时,为了增强防盗用效果,文本水印通常会采用字体、颜色、角度等多种组合方式,使得水印难以被删除或篡改,有效地降低了盗用意愿和风险。 开发人员可以使用图像处理技术和编程语言实现文本水印的功能,例如使用Qt的QPainter类进行文本绘制操作,将文本信息嵌入到图片中,
159 1
Qt(C++)开发一款图片防盗用水印制作小工具
|
2月前
|
监控 C++ 容器
【qt】MDI多文档界面开发
【qt】MDI多文档界面开发
58 0
|
19天前
|
开发工具 C++
qt开发技巧与三个问题点
本文介绍了三个Qt开发中的常见问题及其解决方法,并提供了一些实用的开发技巧。
|
1月前
|
2月前
|
C++
C++ Qt开发:QUdpSocket网络通信组件
QUdpSocket是Qt网络编程中一个非常有用的组件,它提供了在UDP协议下进行数据发送和接收的能力。通过简单的方法和信号,可以轻松实现基于UDP的网络通信。不过,需要注意的是,UDP协议本身不保证数据的可靠传输,因此在使用QUdpSocket时,可能需要在应用层实现一些机制来保证数据的完整性和顺序,或者选择在适用的场景下使用UDP协议。
93 2
Qt开发网络嗅探器02
Qt开发网络嗅探器02
|
2月前
|
存储 运维 监控
Qt开发网络嗅探器01
Qt开发网络嗅探器01
|
2月前
|
网络协议 容器
Qt开发网络嗅探器03
Qt开发网络嗅探器03