QDate Class

The QDate class provides date functions. 更多...

头: #include <QDate>
CMake: find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此类的所有函数 可重入 .

公共函数

  QDate (int y , int m , int d )
  QDate ()
QDate addDays (qint64 ndays ) const
QDate addMonths (int nmonths , QCalendar cal ) const
QDate addMonths (int nmonths ) const
QDate addYears (int nyears , QCalendar cal ) const
QDate addYears (int nyears ) const
int day (QCalendar cal ) const
int day () const
int dayOfWeek (QCalendar cal ) const
int dayOfWeek () const
int dayOfYear (QCalendar cal ) const
int dayOfYear () const
int daysInMonth (QCalendar cal ) const
int daysInMonth () const
int daysInYear (QCalendar cal ) const
int daysInYear () const
qint64 daysTo (QDate d ) const
QDateTime endOfDay (Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const
QDateTime endOfDay (const QTimeZone & zone ) const
void getDate (int * year , int * month , int * day ) const
bool isNull () const
bool isValid () const
int month (QCalendar cal ) const
int month () const
bool setDate (int year , int month , int day )
bool setDate (int year , int month , int day , QCalendar cal )
QDateTime startOfDay (Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const
QDateTime startOfDay (const QTimeZone & zone ) const
qint64 toJulianDay () const
QString toString (const QString & format , QCalendar cal = QCalendar()) const
QString toString (Qt::DateFormat format = Qt::TextDate) const
QString toString (QStringView format , QCalendar cal = QCalendar()) const
int weekNumber (int * yearNumber = nullptr) const
int year (QCalendar cal ) const
int year () const

静态公共成员

QDate currentDate ()
QDate fromJulianDay (qint64 jd )
QDate fromString (const QString & string , Qt::DateFormat format = Qt::TextDate)
QDate fromString (QStringView string , Qt::DateFormat format = Qt::TextDate)
QDate fromString (QStringView string , QStringView format , QCalendar cal = QCalendar())
QDate fromString (const QString & string , QStringView format , QCalendar cal = QCalendar())
QDate fromString (const QString & string , const QString & format , QCalendar cal = QCalendar())
bool isLeapYear (int year )
bool isValid (int year , int month , int day )
bool operator!= (QDate lhs , QDate rhs )
bool operator< (QDate lhs , QDate rhs )
QDataStream & operator<< (QDataStream & out , QDate date )
bool operator<= (QDate lhs , QDate rhs )
bool operator== (QDate lhs , QDate rhs )
bool operator> (QDate lhs , QDate rhs )
bool operator>= (QDate lhs , QDate rhs )
QDataStream & operator>> (QDataStream & in , QDate & date )

详细描述

A QDate object represents a particular day, regardless of calendar, locale or other settings used when creating it or supplied by the system. It can report the year, month and day of the month that represent the day with respect to the proleptic Gregorian calendar or any calendar supplied as a QCalendar object. QDate objects should be passed by value rather than by reference to const; they simply package qint64 .

A QDate object is typically created by giving the year, month, and day numbers explicitly. Note that QDate interprets year numbers less than 100 as presented, i.e., as years 1 through 99, without adding any offset. The static function currentDate () creates a QDate object containing the date read from the system clock. An explicit date can also be set using setDate ()。 fromString () function returns a QDate given a string and a date format which is used to interpret the date within the string.

year (), month (),和 day () functions provide access to the year, month, and day numbers. When more than one of these values is needed, it is more efficient to call QCalendar::partsFromDate (), to save repeating (potentially expensive) calendrical calculations.

Also, dayOfWeek () 和 dayOfYear () functions are provided. The same information is provided in textual format by toString (). QLocale can map the day numbers to names, QCalendar can map month numbers to names.

QDate provides a full set of operators to compare two QDate objects where smaller means earlier, and larger means later.

You can increment (or decrement) a date by a given number of days using addDays (). Similarly you can use addMonths () 和 addYears ()。 daysTo () function returns the number of days between two dates.

daysInMonth () 和 daysInYear () functions return how many days there are in this date's month and year, respectively. The isLeapYear () function indicates whether a date is in a leap year. QCalendar can also supply this information, in some cases more conveniently.

备注

注意: All conversion to and from string formats is done using the C locale. For localized conversions, see QLocale .

In the Gregorian calendar, there is no year 0. Dates in that year are considered invalid. The year -1 is the year "1 before Christ" or "1 before common era." The day before 1 January 1 CE, QDate(1, 1, 1), is 31 December 1 BCE, QDate(-1, 12, 31). Various other calendars behave similarly; see QCalendar::hasYearZero ().

有效日期范围

Dates are stored internally as a Julian Day number, an integer count of every day in a contiguous range, with 24 November 4714 BCE in the Gregorian calendar being Julian Day 0 (1 January 4713 BCE in the Julian calendar). As well as being an efficient and accurate way of storing an absolute date, it is suitable for converting a date into other calendar systems such as Hebrew, Islamic or Chinese. The Julian Day number can be obtained using QDate::toJulianDay () and can be set using QDate::fromJulianDay ().

The range of Julian Day numbers that QDate can represent is, for technical reasons, limited to between -784350574879 and 784354017364, which means from before 2 billion BCE to after 2 billion CE. This is more than seven times as wide as the range of dates a QDateTime can represent.

另请参阅 QTime , QDateTime , QCalendar , QDateTime::YearRange , QDateEdit , QDateTimeEdit ,和 QCalendarWidget .

成员函数文档编制

QString QDate:: toString ( QStringView format , QCalendar cal = QCalendar()) const

QString QDate:: toString (const QString & format , QCalendar cal = QCalendar()) const

Returns the date as a string. The format parameter determines the format of the result string. If cal is supplied, it determines the calendar used to represent the date; it defaults to Gregorian.

这些表达式可以使用:

表达式 输出
d The day as a number without a leading zero (1 to 31)
dd The day as a number with a leading zero (01 to 31)
ddd The abbreviated day name ('Mon' to 'Sun').
dddd The long day name ('Monday' to 'Sunday').
M The month as a number without a leading zero (1 to 12)
MM The month as a number with a leading zero (01 to 12)
MMM The abbreviated month name ('Jan' to 'Dec').
MMMM The long month name ('January' to 'December').
yy The year as a two digit number (00 to 99)
yyyy The year as a four digit number. If the year is negative, a minus sign is prepended, making five characters.

Any sequence of characters enclosed in single quotes will be included verbatim in the output string (stripped of the quotes), even if it contains formatting characters. Two consecutive single quotes ("''") are replaced by a single quote in the output. All other characters in the format string are included verbatim in the output string.

Formats without separators (e.g. "ddMM") are supported but must be used with care, as the resulting strings aren't always reliably readable (e.g. if "dM" produces "212" it could mean either the 2nd of December or the 21st of February).

格式字符串范例 (假定 QDate is the 20 July 1969):

格式 结果
dd.MM.yyyy 20.07.1969
ddd MMMM d yy Sun July 20 69
'The day is' dddd The day is Sunday

若日期时间无效,返回空字符串。

注意: Day and month names are given in English (C locale). If localized month and day names are desired, use QLocale::system (). toString ().

另请参阅 fromString (), QDateTime::toString (), QTime::toString (),和 QLocale::toString ().

[since 5.14] QDateTime QDate:: endOfDay ( Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const

[since 5.14] QDateTime QDate:: endOfDay (const QTimeZone & zone ) const

Returns the end-moment of the day. Usually, this is one millisecond before the midnight at the end of the day: however, if a time-zone transition causes the given date to skip over that midnight (e.g. a DST spring-forward skipping from just before 23:00 to the start of the next day), the actual latest time in the day is returned. This can only arise when the start-moment is specified in terms of a time-zone (by passing its QTimeZone as zone ) or in terms of local time (by passing Qt::LocalTime as spec ; this is its default).

offsetSeconds is ignored unless spec is Qt::OffsetFromUTC , when it gives the implied zone's offset from UTC. As UTC and such zones have no transitions, the end of the day is QTime (23, 59, 59, 999) in these cases.

In the rare case of a date that was entirely skipped (this happens when a zone east of the international date-line switches to being west of it), the return shall be invalid. Passing Qt::TimeZone as spec (instead of passing a QTimeZone ) will also produce an invalid result, as shall dates that end outside the range representable by QDateTime .

该函数在 Qt 5.14 引入。

另请参阅 startOfDay ().

[since 5.14] QDateTime QDate:: startOfDay ( Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const

[since 5.14] QDateTime QDate:: startOfDay (const QTimeZone & zone ) const

Returns the start-moment of the day. Usually, this shall be midnight at the start of the day: however, if a time-zone transition causes the given date to skip over that midnight (e.g. a DST spring-forward skipping from the end of the previous day to 01:00 of the new day), the actual earliest time in the day is returned. This can only arise when the start-moment is specified in terms of a time-zone (by passing its QTimeZone as zone ) or in terms of local time (by passing Qt::LocalTime as spec ; this is its default).

offsetSeconds is ignored unless spec is Qt::OffsetFromUTC , when it gives the implied zone's offset from UTC. As UTC and such zones have no transitions, the start of the day is QTime (0, 0) in these cases.

In the rare case of a date that was entirely skipped (this happens when a zone east of the international date-line switches to being west of it), the return shall be invalid. Passing Qt::TimeZone as spec (instead of passing a QTimeZone ) or passing an invalid time-zone as zone will also produce an invalid result, as shall dates that start outside the range representable by QDateTime .

该函数在 Qt 5.14 引入。

另请参阅 endOfDay ().

QDate:: QDate ( int y , int m , int d )

Constructs a date with year y , month m and day d .

The date is understood in terms of the Gregorian calendar. If the specified date is invalid, the date is not set and isValid () 返回 false .

警告: Years 1 to 99 are interpreted as is. Year 0 is invalid.

另请参阅 isValid () 和 QCalendar::dateFromParts ().

QDate:: QDate ()

Constructs a null date. Null dates are invalid.

另请参阅 isNull () 和 isValid ().

QDate QDate:: addDays ( qint64 ndays ) const

返回 QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).

Returns a null date if the current date is invalid or the new date is out of range.

另请参阅 addMonths (), addYears (),和 daysTo ().

QDate QDate:: addMonths ( int nmonths , QCalendar cal ) const

返回 QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).

Uses cal as calendar, if supplied, else the Gregorian calendar.

注意: If the ending day/month combination does not exist in the resulting month/year, this function will return a date that is the latest valid date in the selected month.

另请参阅 addDays () 和 addYears ().

QDate QDate:: addMonths ( int nmonths ) const

这是重载函数。

QDate QDate:: addYears ( int nyears , QCalendar cal ) const

返回 QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative).

Uses cal as calendar, if supplied, else the Gregorian calendar.

注意: If the ending day/month combination does not exist in the resulting year (e.g., for the Gregorian calendar, if the date was Feb 29 and the final year is not a leap year), this function will return a date that is the latest valid date in the given month (in the example, Feb 28).

另请参阅 addDays () 和 addMonths ().

QDate QDate:: addYears ( int nyears ) const

这是重载函数。

[static] QDate QDate:: currentDate ()

Returns the current date, as reported by the system clock.

另请参阅 QTime::currentTime () 和 QDateTime::currentDateTime ().

int QDate:: day ( QCalendar cal ) const

Returns the day of the month for this date.

Uses cal as calendar if supplied, else the Gregorian calendar (for which the return ranges from 1 to 31). Returns 0 if the date is invalid.

另请参阅 year (), month (), dayOfWeek (),和 QCalendar::partsFromDate ().

int QDate:: day () const

这是重载函数。

int QDate:: dayOfWeek ( QCalendar cal ) const

Returns the weekday (1 = Monday to 7 = Sunday) for this date.

Uses cal as calendar if supplied, else the Gregorian calendar. Returns 0 if the date is invalid. Some calendars may give special meaning (e.g. intercallary days) to values greater than 7.

另请参阅 day (), dayOfYear (), QCalendar::dayOfWeek (),和 Qt::DayOfWeek .

int QDate:: dayOfWeek () const

这是重载函数。

int QDate:: dayOfYear ( QCalendar cal ) const

Returns the day of the year (1 for the first day) for this date.

Uses cal as calendar if supplied, else the Gregorian calendar. Returns 0 if either the date or the first day of its year is invalid.

另请参阅 day (), dayOfWeek (),和 QCalendar::daysInYear ().

int QDate:: dayOfYear () const

这是重载函数。

int QDate:: daysInMonth ( QCalendar cal ) const

Returns the number of days in the month for this date.

Uses cal as calendar if supplied, else the Gregorian calendar (for which the result ranges from 28 to 31). Returns 0 if the date is invalid.

另请参阅 day (), daysInYear (), QCalendar::daysInMonth (), QCalendar::maximumDaysInMonth (),和 QCalendar::minimumDaysInMonth ().

int QDate:: daysInMonth () const

这是重载函数。

int QDate:: daysInYear ( QCalendar cal ) const

Returns the number of days in the year for this date.

Uses cal as calendar if supplied, else the Gregorian calendar (for which the result is 365 or 366). Returns 0 if the date is invalid.

另请参阅 day (), daysInMonth (), QCalendar::daysInYear (),和 QCalendar::maximumMonthsInYear ().

int QDate:: daysInYear () const

这是重载函数。

qint64 QDate:: daysTo ( QDate d ) const

Returns the number of days from this date to d (which is negative if d is earlier than this date).

Returns 0 if either date is invalid.

范例:

QDate d1(1995, 5, 17);  // May 17, 1995
QDate d2(1995, 5, 20);  // May 20, 1995
d1.daysTo(d2);          // returns 3
d2.daysTo(d1);          // returns -3
					

另请参阅 addDays ().

[static] QDate QDate:: fromJulianDay ( qint64 jd )

Converts the Julian day jd QDate .

另请参阅 toJulianDay ().

[static] QDate QDate:: fromString (const QString & string , Qt::DateFormat format = Qt::TextDate)

返回 QDate 表示通过 string ,使用 format given, or an invalid date if the string cannot be parsed.

Note for Qt::TextDate : only English month names (e.g. "Jan" in short form or "January" in long form) are recognized.

另请参阅 toString () 和 QLocale::toDate ().

[static, since 6.0] QDate QDate:: fromString ( QStringView string , Qt::DateFormat format = Qt::TextDate)

这是重载函数。

该函数在 Qt 6.0 引入。

[static, since 6.0] QDate QDate:: fromString ( QStringView string , QStringView format , QCalendar cal = QCalendar())

这是重载函数。

该函数在 Qt 6.0 引入。

[static, since 6.0] QDate QDate:: fromString (const QString & string , QStringView format , QCalendar cal = QCalendar())

这是重载函数。

该函数在 Qt 6.0 引入。

[static] QDate QDate:: fromString (const QString & string , const QString & format , QCalendar cal = QCalendar())

返回 QDate 表示通过 string ,使用 format given, or an invalid date if the string cannot be parsed.

Uses cal as calendar if supplied, else the Gregorian calendar. Ranges of values in the format descriptions below are for the latter; they may be different for other calendars.

These expressions may be used for the format:

表达式 输出
d The day as a number without a leading zero (1 to 31)
dd The day as a number with a leading zero (01 to 31)
ddd The abbreviated day name ('Mon' to 'Sun').
dddd The long day name ('Monday' to 'Sunday').
M The month as a number without a leading zero (1 to 12)
MM The month as a number with a leading zero (01 to 12)
MMM The abbreviated month name ('Jan' to 'Dec').
MMMM The long month name ('January' to 'December').
yy The year as a two digit number (00 to 99)
yyyy The year as a four digit number, possibly plus a leading minus sign for negative years.

注意: Day and month names must be given in English (C locale). If localized month and day names are used, use QLocale::system ().toDate().

All other input characters will be treated as text. Any non-empty sequence of characters enclosed in single quotes will also be treated (stripped of the quotes) as text and not be interpreted as expressions. For example:

QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy");
// date is 1 December 2003
					

If the format is not satisfied, an invalid QDate is returned. The expressions that don't expect leading zeroes (d, M) will be greedy. This means that they will use two digits even if this will put them outside the accepted range of values and leaves too few digits for other sections. For example, the following format string could have meant January 30 but the M will grab two digits, resulting in an invalid date:

QDate date = QDate::fromString("130", "Md"); // invalid
					

For any field that is not represented in the format the following defaults are used:

Field 默认值
Year 1900
Month 1 (January)
Day 1

The following examples demonstrate the default values:

QDate::fromString("1.30", "M.d");           // January 30 1900
QDate::fromString("20000110", "yyyyMMdd");  // January 10, 2000
QDate::fromString("20000110", "yyyyMd");    // January 10, 2000
					

另请参阅 toString (), QDateTime::fromString (), QTime::fromString (),和 QLocale::toDate ().

void QDate:: getDate ( int * year , int * month , int * day ) const

Extracts the date's year, month, and day, and assigns them to * year , * month , and * day . The pointers may be null.

Returns 0 if the date is invalid.

注意: In Qt versions prior to 5.7, this function is marked as non- const .

另请参阅 year (), month (), day (), isValid (),和 QCalendar::partsFromDate ().

[static] bool QDate:: isLeapYear ( int year )

返回 true if the specified year is a leap year in the Gregorian calendar; otherwise returns false .

另请参阅 QCalendar::isLeapYear ().

bool QDate:: isNull () const

返回 true if the date is null; otherwise returns false . A null date is invalid.

注意: The behavior of this function is equivalent to isValid ().

另请参阅 isValid ().

bool QDate:: isValid () const

返回 true if this date is valid; otherwise returns false .

另请参阅 isNull () 和 QCalendar::isDateValid ().

[static] bool QDate:: isValid ( int year , int month , int day )

这是重载函数。

返回 true if the specified date ( year , month ,和 day ) is valid in the Gregorian calendar; otherwise returns false .

范例:

QDate::isValid(2002, 5, 17);  // true
QDate::isValid(2002, 2, 30);  // false (Feb 30 does not exist)
QDate::isValid(2004, 2, 29);  // true (2004 is a leap year)
QDate::isValid(2000, 2, 29);  // true (2000 is a leap year)
QDate::isValid(2006, 2, 29);  // false (2006 is not a leap year)
QDate::isValid(2100, 2, 29);  // false (2100 is not a leap year)
QDate::isValid(1202, 6, 6);   // true (even though 1202 is pre-Gregorian)
					

另请参阅 isNull (), setDate (),和 QCalendar::isDateValid ().

int QDate:: month ( QCalendar cal ) const

Returns the month-number for the date.

Numbers the months of the year starting with 1 for the first. Uses cal as calendar if supplied, else the Gregorian calendar, for which the month numbering is as follows:

  • 1 = "January"
  • 2 = "February"
  • 3 = "March"
  • 4 = "April"
  • 5 = "May"
  • 6 = "June"
  • 7 = "July"
  • 8 = "August"
  • 9 = "September"
  • 10 = "October"
  • 11 = "November"
  • 12 = "December"

Returns 0 if the date is invalid. Note that some calendars may have more than 12 months in some years.

另请参阅 year (), day (),和 QCalendar::partsFromDate ().

int QDate:: month () const

这是重载函数。

bool QDate:: setDate ( int year , int month , int day )

Sets this to represent the date, in the Gregorian calendar, with the given year , month and day numbers. Returns true if the resulting date is valid, otherwise it sets this to represent an invalid date and returns false.

另请参阅 isValid () 和 QCalendar::dateFromParts ().

[since 5.14] bool QDate:: setDate ( int year , int month , int day , QCalendar cal )

Sets this to represent the date, in the given calendar cal ,采用给定 year , month and day numbers. Returns true if the resulting date is valid, otherwise it sets this to represent an invalid date and returns false.

该函数在 Qt 5.14 引入。

另请参阅 isValid () 和 QCalendar::dateFromParts ().

qint64 QDate:: toJulianDay () const

Converts the date to a Julian day.

另请参阅 fromJulianDay ().

QString QDate:: toString ( Qt::DateFormat format = Qt::TextDate) const

这是重载函数。

Returns the date as a string. The format 参数确定字符串的格式。

format is Qt::TextDate , the string is formatted in the default way. The day and month names will be in English. An example of this formatting is "Sat May 20 1995". For localized formatting, see QLocale::toString ().

format is Qt::ISODate , the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form yyyy-MM-dd, where yyyy is the year, MM is the month of the year (between 01 and 12), and dd is the day of the month between 01 and 31.

format is Qt::RFC2822Date , the string is formatted in an RFC 2822 compatible way. An example of this formatting is "20 May 1995".

If the date is invalid, an empty string will be returned.

警告: Qt::ISODate format is only valid for years in the range 0 to 9999.

另请参阅 fromString () 和 QLocale::toString ().

int QDate:: weekNumber ( int * yearNumber = nullptr) const

Returns the ISO 8601 week number (1 to 53).

Returns 0 if the date is invalid. Otherwise, returns the week number for the date. If yearNumber 不是 nullptr (its default), stores the year as * yearNumber .

In accordance with ISO 8601, each week falls in the year to which most of its days belong, in the Gregorian calendar. As ISO 8601's week starts on Monday, this is the year in which the week's Thursday falls. Most years have 52 weeks, but some have 53.

注意: * yearNumber is not always the same as year (). For example, 1 January 2000 has week number 52 in the year 1999, and 31 December 2002 has week number 1 in the year 2003.

另请参阅 isValid ().

int QDate:: year ( QCalendar cal ) const

返回此日期的年数。

Uses cal as calendar, if supplied, else the Gregorian calendar.

Returns 0 if the date is invalid. For some calendars, dates before their first year may all be invalid.

If using a calendar which has a year 0, check using isValid () if the return is 0. Such calendars use negative year numbers in the obvious way, with year 1 preceded by year 0, in turn preceded by year -1 and so on.

Some calendars, despite having no year 0, have a conventional numbering of the years before their first year, counting backwards from 1. For example, in the proleptic Gregorian calendar, successive years before 1 CE (the first year) are identified as 1 BCE, 2 BCE, 3 BCE and so on. For such calendars, negative year numbers are used to indicate these years before year 1, with -1 indicating the year before 1.

另请参阅 month (), day (), QCalendar::hasYearZero (), QCalendar::isProleptic (),和 QCalendar::partsFromDate ().

int QDate:: year () const

这是重载函数。

相关非成员

bool operator!= ( QDate lhs , QDate rhs )

返回 true if lhs and rhs represent distinct days; otherwise returns false .

另请参阅 operator== ().

bool operator< ( QDate lhs , QDate rhs )

返回 true if lhs is earlier than rhs ;否则返回 false .

QDataStream & operator<< ( QDataStream & out , QDate date )

写入 date 到流 out .

另请参阅 序列化 Qt 数据类型 .

bool operator<= ( QDate lhs , QDate rhs )

返回 true if lhs is earlier than or equal to rhs ;否则返回 false .

bool operator== ( QDate lhs , QDate rhs )

返回 true if lhs and rhs represent the same day, otherwise false .

bool operator> ( QDate lhs , QDate rhs )

返回 true if lhs is later than rhs ;否则返回 false .

bool operator>= ( QDate lhs , QDate rhs )

返回 true if lhs is later than or equal to rhs ;否则返回 false .

QDataStream & operator>> ( QDataStream & in , QDate & date )

Reads a date from stream in date .

另请参阅 序列化 Qt 数据类型 .