Qt中的日期和时间

news/2024/10/17 20:26:45/

目录

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-01QString 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 ~ 23m and s 	==> 取值范围: 0 ~ 59ms 		==> 取值范围: 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:00QTime t;t = n.addSecs(70);                // t == 14:01:10t = n.addSecs(-70);               // t == 13:58:50t = n.addSecs(10 * 60 * 60 + 5);  // t == 00:00:05t = 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();//方式1qDebug()<<"hour: "<<curtime.hour() <<",minute: "<<curtime.minute()<<", second: "<<curtime.second()<<" , millisecond: "<<curtime.msec();//方式2QString 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 apQString 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中会使用到的时间和日期的相关知识 


http://www.ppmy.cn/news/533626.html

相关文章

【算法证明 五】并查集的时间复杂度

相信如果不是为了刷 leetcode 外&#xff0c;很少有数据结构的数介绍并查集这一数据结构的。并查集的算法模板写起来非常容易&#xff0c;所以刷了并查集相关算法题的人&#xff0c;应该也不会去深入分析这一数据结构&#xff0c;最多知道路径压缩、按秩合并可以做到非常快。深…

基于边界点优化和多步路径规划的机器人自主探索

论文题目&#xff1a;Autonomous Robotic Exploration Based on Frontier Point Optimization and Multistep Path Planning 中文题目&#xff1a;基于边界点优化和多步路径规划的机器人自主探索 作者&#xff1a;Baofu Fang &#xff1b;Jianfeng Ding ; Zaijun Wang 作者机…

canvas详解08-基本动画

由于我们是用 JavaScript 去操控 canvas 对象,这样要实现一些交互动画也是相当容易的。在本章中,我们将看看如何做一些基本的动画。 可能最大的限制就是图像一旦绘制出来,它就是一直保持那样了。如果需要移动它,我们不得不对所有东西(包括之前的)进行重绘。重绘是相当费…

tr,td设置高度不生效

功能&#xff1a;表格内容较长&#xff0c;但是页面高度有限&#xff0c;超出显示滚动条 阻碍&#xff1a;给tr或者td加高度都不生效&#xff0c;不显示滚动条 解决方案&#xff1a;td中加div&#xff0c;设置高度和内容溢出时的样式 <table border1 width300><tr&…

html tr能不能设置宽高,html中表格tr的td单元格怎么设置宽度属性

table的宽度是自适应的&#xff0c;而且部分TD是固定宽度。原则上应该讲table的宽度设置成一个固定的值&#xff0c;而不应该设置成一个根据屏幕变化的值。现在来看下如何设置表格td单元格的宽度。 例1&#xff1a;Table的宽度为600px,Table的td所有宽度总和不到600px,浏览器会…

3GPP SI/WI查询、对应的TS/TR查询、会议提案查询

背景 近期在研究R-17版本时需要查看新版本的特性&#xff0c;需要找到感兴趣的study item或work item&#xff0c;以及对应的TS/TR&#xff0c;还需要查看会议记录找到讨论的提案&#xff0c;本文简单介绍一下本人的查找过程。 步骤 1. 先在官网找到Work plan&#xff0c;然…

看好豪爵的摩托车,程序员也要放飞自我,想做的事情就要去做,省的以后没有机会而后悔。

1&#xff0c;豪爵 gzs150 在摩博会上已经有样车了 https://www.dongchedi.com/video/7049541643745264140 前后碟刹&#xff0c;双通道 ABS&#xff0c;LED大灯&#xff0c;风冷单缸&#xff0c;DR160 的同款机器。 轮胎尺寸&#xff0c;从原来的后120/80-16变成了现在的后1…

Table tr td th表格使用案例

caption 属性返回了表格的 caption 元素。 caption 元素定义了表格的标题。 注意&#xff1a; <caption> 元素定义了一个表格标题。<caption> 标签必须紧跟在 <table> 标签之后&#xff0c;每个表格仅能规定一个 caption。通常&#xff0c;caption 会位于表格…