Qt学习(8)——Qt5中的日期(Date)和时间(Time)(2)

本文介绍了Qt5中的日期和时间处理,包括预定义的日期和时间格式,如默认的yyyy-MM-dd、m/d/yyyy和dd.MM.yyyy等,以及如何自定义日期和时间格式,如使用24小时制或12小时制加AM/PM标志。通过示例展示了如何使用Qt5的日期和时间格式化方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

预定义日期格式

Qt5有一些内置的日期格式。QDate对象的toString()方法将日期格式作为参数。Qt5使用的默认日期格式是Qt::TextDate

// dateformats.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

   QTextStream out(stdout);

   QDate cd = QDate::currentDate();

   // 八种日期格式化方式
   out << "Today is " << cd.toString(Qt::TextDate) << endl;
   out << "Today is " << cd.toString(Qt::ISODate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleShortDate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleLongDate) << endl;
   out << "Today is " << cd.toString(Qt::DefaultLocaleShortDate) << endl;
   out << "Today is " << cd.toString(Qt::DefaultLocaleLongDate) << endl;
   out << "Today is " << cd.toString(Qt::SystemLocaleDate) << endl;
   out << "Today is " << cd.toString(Qt::LocaleDate) << endl;   
}

输出结果为:

$ ./dateformats 
Today is 周四 215 2018
Today is 2018-02-15
Today is 2018/2/15
Today is 2018215日星期四
Today is 2018/2/15
Today is 2018215日星期四
Today is 2018/2/15
Today is 2018/2/15

自定义日期格式

日期可以用各种其他格式表示。在Qt5中,我们也可以创建自定义日期格式。toString()方法的另一个版本采用格式字符串,我们可以使用各种格式说明符。例如,d说明符代表一天的数字,而不是前导零。dd说明符代表一天的前导零的数字。下表列出了可用的日期格式表达式:

表达式输出
d作为没有前导零的数字(1到31)
dd作为前导零的数字(01至31)
ddd缩写本地化日期名称(例如e.g. ‘Mon’ to ‘Sun’,“周一”到“周四”)。使用QDate::shortDayName()
dddd本地化日期较长的名称(例如’Monday’ to ‘Sunday’,“星期一”到“星期日”)。使用QDate::longDayName()
M该月份是一个没有前导零的数字(1到12)
MM该月份作为具有前导零的数字(01至12)
MMM缩写本地化月份名称(例如’Jan’ to ‘Dec’,“1月”到“12月”)。使用QDate::shortMonthName()
MMMM本地化的月份名称(例如 ‘January’ to ‘December’,“一月”到“十二月”)。使用qdate :: longmonthname()。
yy年份为两位数字(00至99)
yyyy年份为四位数字。如果年份是负值,则会增加一个负号。
// customdateformats.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

   QTextStream out(stdout);

   QDate cd = QDate::currentDate();

   out << "Today is " << cd.toString("yyyy-MM-dd") << endl;
   out << "Today is " << cd.toString("yy/M/dd") << endl;
   out << "Today is " << cd.toString("d. M. yyyy") << endl;
   out << "Today is " << cd.toString("d-MMMM-yyyy") << endl; 
}
out << "Today is " << cd.toString("yyyy-MM-dd") << endl;

这是国际日期格式。日期的部分由短划线字符分隔。yyyy是一个四位数的年份。mm为月份,数字为前导零(01至12)。并dd是以前导零(01到31)的数字表示的日期。

out << "Today is " << cd.toString("yy/M/dd") << endl;

这是另一种常见的日期格式。用斜线(/)字符分隔。m说明符代表一个月,但不包含前导零(1到12)。

out << "Today is " << cd.toString("d. M. yyyy") << endl;

这种日期格式在斯洛伐克使用。部件之间用点字符分隔。日和月没有前导零。首先是一天,然后是本月,最后是一年。
输出结果为:

$ ./customdateformats 
Today is 2018-02-15
Today is 18/2/15
Today is 15. 2. 2018
Today is 15-二月-2018

预定义时间格式

时间有一些预定义的格式。标准格式说明符与日期格式中使用的相同。Qt5使用的默认时间格式是Qt::TextDate

// timeformats.cpp
#include <QTextStream>
#include <QTime>

int main(void) {

   QTextStream out(stdout);

   QTime ct = QTime::currentTime();

   // 八种时间格式
   out << "The time is " << ct.toString(Qt::TextDate) << endl;
   out << "The time is " << ct.toString(Qt::ISODate) << endl;
   out << "The time is " << ct.toString(Qt::SystemLocaleShortDate) << endl;
   out << "The time is " << ct.toString(Qt::SystemLocaleLongDate) << endl;
   out << "The time is " << ct.toString(Qt::DefaultLocaleShortDate) << endl;
   out << "The time is " << ct.toString(Qt::DefaultLocaleLongDate) << endl;
   out << "The time is " << ct.toString(Qt::SystemLocaleDate) << endl;
   out << "The time is " << ct.toString(Qt::LocaleDate) << endl;   
}
out << "The time is " << ct.toString(Qt::ISODate) << endl;

这里我们以Qt::ISODate格式打印当前时间,这是一种显示时间的国际标准。
输出结果为:

$ ./timeformats 
The time is 13:46:14
The time is 13:46:14
The time is 下午1:46
The time is CST 下午1:46:14
The time is 下午1:46
The time is CST 下午1:46:14
The time is 下午1:46
The time is 下午1:46

自定义时间格式

我们可以创建额外的时间格式。我们使用时间格式说明符建立自定义时间格式。下表列出了可用的格式表达式。

表达式输出
h没有前导零的小时(0到23或1到12(AM/PM显示))
hh有前导零的小时(0到23或1到12(AM/PM显示))
H没有前导零的小时(0到23,即使是AM/PM显示)
HH有前导零的小时(0到23,即使是AM/PM显示)
m没有前导零的分钟(0到59)
mm带前导零的分钟(00至59)
s第二个没有前导零(0到59)
ss第二个具有前导零(00到59)
z不带前导零的毫秒(0至999)
zzz带前导零的毫秒(000至999)
AP or A使用AM/PM显示。AP将被替换为“AM”或“PM”。
ap or a使用am/pm显示。ap将被替换为“am”或“pm”。
t时区(例如“CEST”)
// customtimeformats.cpp
#include <QTextStream>
#include <QTime>

int main(void) {

   QTextStream out(stdout);

   QTime ct = QTime::currentTime();

   // 四种格式化方式
   out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl;
   out << "The time is " << ct.toString("h:m:s a") << endl;
   out << "The time is " << ct.toString("H:m:s A") << endl;
   out << "The time is " << ct.toString("h:m AP") << endl;  

   out << "The version of Qt5 is " << qVersion() << endl;
}
out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl;

在这种格式下,我们有小时,分钟和秒,均有前导零。添加拥有前导零的毫秒。

out << "The time is " << ct.toString("h:m:s a") << endl;

此时间格式说明符使用不带前导零的小时,分​​钟和秒,并添加am/pm周期标识符。

$ ./customtimeformats 
The time is 14:07:22.544
The time is 2:7:22 下午
The time is 14:7:22 下午
The time is 2:7 下午
The version of Qt5 is 5.10.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值