Qt的日期和时间
目录
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();
示例(打印年月日):
-
-
-
-
-
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();
示例(显示时分秒):
-
-
-
-
-
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();
示例(显示当前日期和时间):
-
-
-
-
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;
-
}
运行结果:
可以看到当我们采用的格式不同时,得到的日期时间显示结果也不同
示例(分别取出 年 月 日 时 分 秒):
-
-
-
-
-
-
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中会使用到的时间和日期的相关知识
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhghcbbg
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13