【Qt开发】QDate类

QDate为开发者提供日期的类,函数也很丰富

常用方法介绍 

1.QDate addDays(qint64 ndays) const

    当前日期添加n天,n可以为负

2.QDate addMonths(int nmonths) const

    当前日期添加n月,n可以为负

3.QDate addYears(int nyears) const

    当前日期添加n年,n可以为负

4.int day() const

    返回所处月的天数(1到31)

5.int month() const

    返回日期的月数

6.int year() const

    返回日期的年数

7.int dayOfWeek() const

    返回周几(1到7),0表示日期无效

8.int dayOfYear() const

    返回所处年的天数(1~365/366)

9.int daysInMonth() const

    返回当前月共多少天(28~31)

10.int daysInYear() const

    返回当前年共多少天(365/366)

11.qint64 daysTo(const QDate &d) const

    返回当前日期到d的天数,可为负

12.void getDate(int *year, int *month, int *day) const

    提取日期的年、月和日,并将它们分配给*年、*月和*日。指针可以为空。

13.bool isNull() const

    判断日期对象是否为NULL

14.bool isValid() const

    判断日期对象是否有效,如月的范围(1~12),不在此范围为无效日期

15.bool setDate(int year, int month, int day)

    设置日期

16.QDate static currentDate()

    获得当前日期

17.bool static isLeapYear(int year)

    判断year是否为闰年

18.QString toString(const QString &format) const

    按照format格式转换成日期QString

19.QDate static fromString(const QString & string, const QString & format)

    按照规则生成QDate

时间格式显示


d    一位表示天数(1到31)

dd   两位表示天数(01到31)

ddd   英文简写表示('Mon' to 'Sun')

dddd  英文全写表示( 'Monday' to 'Sunday')

M    一位表示月数(1到12)

MM    两位表示月数(01到12)

MMM   英文简写表示 ('Jan' to 'Dec')

MMMM  英文全写表示 ( 'January' to 'December')

yy   两位表示年数(00 to 99)

yyyy  年数用四位数表示,如年为负数,则在前面加上一个负号

例:

dd.MM.yyyy       21.08.2016

ddd MMMM d yy     Sun July 20 69

'The day is' dddd   The day is Sunday

原文地址:https://www.cnblogs.com/woniu201/p/10755884.html