检索星期
dayOfWeek()
方法返回一个数字,表示一周的某一天,其中1是星期一,7是星期日。
// weekday.cpp
#include <QTextStream>
#include <QDate>
int main(void) {
QTextStream out(stdout);
QDate cd = QDate::currentDate(); // 获取当前日期
int wd = cd.dayOfWeek(); // 得到今天是星期几
out << "Today is " << QDate::shortDayName(wd) << endl;
out << "Today is " << QDate::longDayName(wd) << endl;
}
使用QDate::shortDayName()
静态方法,得到了周二的短名称。
使用QDate::longDayName()
静态方法,得到了周二的全称。
输出结果:
$ ./weekday
Today is 周二
Today is 星期二
注:
在执行make
之后,会有如下提示:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I../../../../Qt/5.10.0/gcc_64/include -I../../../../Qt/5.10.0/gcc_64/include/QtGui -I../../../../Qt/5.10.0/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I../../../../Qt/5.10.0/gcc_64/mkspecs/linux-g++ -o weekday.o weekday.cpp
weekday.cpp: In function ‘int main()’:
weekday.cpp:11:33: warning: ‘static QString QDate::shortDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations]
out << "Today is " << QDate::shortDayName(wd) << endl;
^~~~~~~~~~~~
In file included from ../../../../Qt/5.10.0/gcc_64/include/QtCore/QDate:1:0,
from weekday.cpp:2:
../../../../Qt/5.10.0/gcc_64/include/QtCore/qdatetime.h:88:24: note: declared here
static QString shortDayName(int weekday, MonthNameType type = DateFormat);
^~~~~~~~~~~~
weekday.cpp:11:48: w