預定義日期格式
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 週四 2月 15 2018
Today is 2018-02-15
Today is 2018/2/15
Today is 2018年2月15日星期四
Today is 2018/2/15
Today is 2018年2月15日星期四
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