5 时间和日期
约 2503 字大约 8 分钟
2025-06-22
表示日期时间数据的类有:
- QTime:时间数据类型,如15:23:13。
- QDate:日期数据类型,如2017-4-5。
- QDateTime:日期时间数据类型,如2017-03-23 08:12:43
- QCalendarWidget:一个用日历形式选择日期的组件。 相应的编辑器有QTimeEdit,QDateEdit,QDateTimeEdit。
格式字符的意义
格式字符 | 意义 |
---|---|
d | 天,不补零显示,1-31 |
dd | 天,补零显示,01-31M |
M | 月,不补零显示,1-12 |
MM | 月,补零显示,01-12 |
yy | 年,两位显示,00-99 |
yyyy | 年,4位数字显示,如2016 |
h | 小时,不补零,0-23或1-12(如果显示AM/PM) |
hh | 小时,补零2位显示,00-23或01-12(如果显示AM/PM) |
H | 小时,不补零,0-23 (即使显示 AM/PM) |
HH | 小时,补零显示,00-23(即使显示 AM/PM) |
m | 分钟,不补零,0-59 |
mm | 分钟,补零显示,00-59 |
s | 秒,不补零显示,0-59 |
ss | 秒,补零显示,00-59 |
z | 毫秒,不补零,0-999 |
zzz | 毫秒,补零3位显示,000-999 |
AP或A | 使用AM/PM显示 |
ap或a | 使用am/pm 显示 |
QDate日期
// 构造函数
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)
QString QDate::toString(const QString &format) const;
//如:date.toString("yyyy-MM-dd");将日期指定为特定格式其中-也是可以更改的,更改的什么格式化出的日期字符串就是以什么为间隔
// 操作符重载 ==> 日期比较
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();
QTime时间
QTime类可以封装时间信息也可以通过这个类得到时间相关的信息, 包括:时, 分, 秒, 毫秒。
// 构造函数
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();
QTimer和QElapsedTimer
QTimer是软件定时器,QTimer的主要功能是设置一个以毫秒为单位的定时周期,然后进行连续定时或单次定时。 active:激活状态,bool值 singleShot:bool linterval:周期,int值 remainingTime:还剩多少时间,const int值 timer Type:QtPreciseTimer:value为O,精度保持在毫秒级别 Qt:CoarseTimer:value为1,精度保持在所需间隔的5%以内 QtsingleShot静态函数,可以方便的调用一次slot函数。 QElapsedTimer用于快速计算两个事件之间的间隔时间,是软件计时器。
在进行窗口程序的处理过程中, 经常要周期性的执行某些操作, 或者制作一些动画效果,看似比较复杂的问题使用定时器就可以完美的解决这些问题, Qt中提供了两种定时器方式一种是使用Qt中的事件处理函数这个在后续章节会给大家做细致的讲解,本节主要给大家介绍一下Qt中的定时器类 QTimer的使用方法。
QTimer
要使用它,只需创建一个QTimer类对象,然后调用其 start() 函数开启定时器,此后QTimer对象就会周期性的发出 timeout() 信号。
常用API
public/slot function
// 构造函数
// 如果指定了父对象, 创建的堆内存可以自动析构
QTimer::QTimer(QObject *parent = nullptr);
// 设置定时器时间间隔为 msec 毫秒
// 默认值是0,一旦窗口系统事件队列中的所有事件都已经被处理完,一个时间间隔为0的QTimer就会触发
void QTimer::setInterval(int msec);
// 获取定时器的时间间隔, 返回值单位: 毫秒
int QTimer::interval() const;
// 根据指定的时间间隔启动或者重启定时器, 需要调用 setInterval() 设置时间间隔
[slot] void QTimer::start();
// 启动或重新启动定时器,超时间隔为msec毫秒。
[slot] void QTimer::start(int msec);
// 停止定时器。
[slot] void QTimer::stop();
// 设置定时器精度
/*
参数:
- Qt::PreciseTimer -> 精确的精度, 毫秒级
- Qt::CoarseTimer -> 粗糙的精度, 和1毫秒的误差在5%的范围内, 默认精度
- Qt::VeryCoarseTimer -> 非常粗糙的精度, 精度在1秒左右
*/
void QTimer::setTimerType(Qt::TimerType atype);
Qt::TimerType QTimer::timerType() const; // 获取当前定时器的精度
// 如果定时器正在运行,返回true; 否则返回false。
bool QTimer::isActive() const;
// 判断定时器是否只触发一次
bool QTimer::isSingleShot() const;
// 设置定时器是否只触发一次, 参数为true定时器只触发一次, 为false定时器重复触发, 默认为false
void QTimer::setSingleShot(bool singleShot);
signals
[signal] void QTimer::timeout();
static public function
// 其他同名重载函数可以自己查阅帮助文档
/*
功能: 在msec毫秒后发射一次信号, 并且只发射一次
参数:
- msec: 在msec毫秒后发射信号
- receiver: 接收信号的对象地址
- method: 槽函数地址
*/
[static] void QTimer::singleShot(
int msec, const QObject *receiver,
PointerToMemberFunction method);
QDateTime
QDateTime类可以封装日期和时间信息也可以通过这个类得到日期和时间相关的信息, 包括:年, 月, 日, 时, 分, 秒, 毫秒。其实这个类就是QDate 和 QTime 这两个类的结合体。
// 构造函数
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();
贡献者
版权所有
版权归属:PinkDopeyBug