Qt学习(9)——Qt5中的日期(Date)和时间(Time)(3)
2018-02-20 10:54
459 查看
检索星期
计算天数
检查日期的有效性
计算指定日期N天后的日期
使用
使用
输出结果:
注:
在执行
可以看出来,
不过应用这两个方法,需要
输出结果为:
使用
输出结果为:
输出结果为:
输出结果为:
计算天数
检查日期的有效性
计算指定日期N天后的日期
检索星期
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: 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:12:33: warning: ‘static QString QDate::longDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations] out << "Today is " << QDate::longDayName(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:92:24: note: declared here static QString longDayName(int weekday, MonthNameType type = DateFormat); ^~~~~~~~~~~ weekday.cpp:12:47: warning: ‘static QString QDate::longDayName(int, QDate::MonthNameType)’ is deprecated: Use QLocale::dayName or QLocale::standaloneDayName [-Wdeprecated-declarations] out << "Today is " << QDate::longDayName(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:92:24: note: declared here static QString longDayName(int weekday, MonthNameType type = DateFormat); ^~~~~~~~~~~ g++ -Wl,-O1 -Wl,-rpath,/home/zhangsl/Qt/5.10.0/gcc_64/lib -o weekday weekday.o -L/home/zhangsl/Qt/5.10.0/gcc_64/lib -lQt5Gui -lQt5Core -lGL -lpthread
可以看出来,
QDate::shortDayName()、
QDate::longDayName()已经被舍弃了,可以应用
QLocale::dayName或
QLocale::standaloneDayName方法。
不过应用这两个方法,需要
#include <QLocale>,并且在
.pro文件内加上
QT += core。
// weekdatnew.cpp #include <QTextStream> #include <QDate> #include <QLocale> int main(void) { QTextStream out(stdout); out << "Today is " << QLocale().dayName(QDate::currentDate().dayOfWeek(), QLocale::LongFormat) << endl; out << "Today is " << QLocale().dayName(QDate::currentDate().dayOfWeek(), QLocale::ShortFormat) << endl; out << "Today is " << QLocale().standaloneDayName(QDate::currentDate().dayOfWeek(), QLocale::LongFormat) << endl; out << "Today is " << QLocale().standaloneDayName(QDate::currentDate().dayOfWeek(), QLocale::ShortFormat) << endl; }
输出结果为:
$ ./weekdaynew Today is 星期二 Today is 周二 Today is 星期二 Today is 周二
计算天数
可以使用daysInMonth()方法计算特定月份中的天数,并使用
daysInYear()方法计算一年中的天数。
// nofdays.cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QList<QString> months; months.append("January"); months.append("February"); months.append("March"); months.append("April"); months.append("May"); months.append("June"); months.append("July"); months.append("August"); months.append("September"); months.append("October"); months.append("November"); months.append("December"); QDate dt1(2015, 9, 18); QDate dt2(2015, 2, 11); QDate dt3(2015, 5, 1); QDate dt4(2015, 12, 11); QDate dt5(2015, 1, 21); // 获取月份中的天数 out << "There are " << dt1.daysInMonth() << " days in " << months.at(dt1.month()-1) << endl; out << "There are " << dt2.daysInMonth() << " days in " << months.at(dt2.month()-1) << endl; out << "There are " << dt3.daysInMonth() << " days in " << months.at(dt3.month()-1) << endl; out << "There are " << dt4.daysInMonth() << " days in " << months.at(dt4.month()-1) << endl; out << "There are " << dt5.daysInMonth() << " days in " << months.at(dt5.month()-1) << endl; // 获取一年中的天数 out << "There are " << dt1.daysInYear() << " days in year " << QString::number(dt1.year()) << endl; }
使用
daysInMonth()获取指定月份的天数,使用
daysInYear()获取指定年份的天数。
输出结果为:
$ ./nofdays There are 30 days in September There are 28 days in February There are 31 days in May There are 31 days in December There are 31 days in January There are 365 days in year 2015
检查日期的有效性
使用isValid()方法检查日期是否有效。
// validity.cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); // 创建三个日期,前两个有效,后一个无效 QList<QDate> dates({QDate(2015, 5, 11), QDate(2015, 8, 1), QDate(2015, 2, 30)}); for (int i=0; i < dates.size(); i++) { if (dates.at(i).isValid()) { out << "Date " << i+1 << " is a valid date" << endl; } else { out << "Date " << i+1 << " is not a valid date" << endl; } } }
输出结果为:
$ ./validity Date 1 is a valid date Date 2 is a valid date Date 3 is not a valid date
计算指定日期N天后的日期
我们可以轻松计算出特定日期n天后的日期。使用addDays()方法。
daysTo()方法返回选定日期的天数。
// daystofrom.cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate dt(2018, 2, 20); // addDays()方法返回给定日期后55天的QDate日期。 QDate nd = dt.addDays(55); QDate xmas(2018, 12, 24); out << "55 days from " << dt.toString() << " is " << nd.toString() << endl; // 我们使用daysTo()方法计算圣诞节前的天数。 out << "There are " << QDate::currentDate().daysTo(xmas) << " days till Christmas" << endl; }
输出结果为:
$ ./daystofrom 55 days from 周二 2月 20 2018 is 周一 4月 16 2018 There are 307 days till Christmas
相关文章推荐
- Qt学习(8)——Qt5中的日期(Date)和时间(Time)(2)
- Qt学习(10)——Qt5中的日期(Date)和时间(Time)(4)
- Qt学习(7)——Qt5中的日期(Date)和时间(Time)(1)
- qt4 设置系统日期 时间,set time date, set system date time
- 8.Android之日期DatePicker和时间TimeTicker控件学习
- Java8学习笔记(九)--日期/时间(Date Time)API指南
- Qt学习(11)——Qt5中的日期(Date)和时间(Time)总结
- 跟燕十八学习PHP-第二十三天-mysql日期时间列详解date-time-year-timesta
- qt4 设置系统日期 时间,set time date, set system date time
- 对话框之AlertDialog、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择)、ProgressDialog(进度)、PopupWindow
- Android时间日期拾取器学习使用(DatePicker、TimePicker)
- 第一好用的时间 日期插件(Adding a Timepicker to jQuery UI Datepicker)
- Python学习笔记9:标准库之日期时间(time包,datetime包)
- 批处理date 和 time日期和时间
- JAVA处理日期(Date)时间(Time)以及相关类的介绍
- 对于社保截止日接口的学习记录---对于php时间的操作(date_time和timestamp)
- 各种杂项组件(3)之--CalendarView(日历视图)、DatePicker/TimePicker(日期、时间选择器)、NumberPicker(数值选择器)
- Python模块学习 ---- time 日期时间处理
- xslt格式化日期和时间:使用ms:format-date、ms:format-time
- LocalDate LocalTime LocalDateTime DateTimeFormatter Java 8 新出的时间(time)和日期(date)的API的使用详细实例