Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QDate Class Reference

\inmodule QtCore \reentrant More...

#include <qdatetime.h>

+ Collaboration diagram for QDate:

Public Member Functions

constexpr QDate ()
 Constructs a null date.
 
 QDate (int y, int m, int d)
 Constructs a date with year y, month m and day d.
 
 QDate (int y, int m, int d, QCalendar cal)
 
constexpr bool isNull () const
 Returns true if the date is null; otherwise returns false.
 
constexpr bool isValid () const
 Returns true if this date is valid; otherwise returns false.
 
int year () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int month () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int day () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int dayOfWeek () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int dayOfYear () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int daysInMonth () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int daysInYear () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
int weekNumber (int *yearNum=nullptr) const
 Returns the ISO 8601 week number (1 to 53).
 
int year (QCalendar cal) const
 Returns the year of this date.
 
int month (QCalendar cal) const
 Returns the month-number for the date.
 
int day (QCalendar cal) const
 Returns the day of the month for this date.
 
int dayOfWeek (QCalendar cal) const
 Returns the weekday (1 = Monday to 7 = Sunday) for this date.
 
int dayOfYear (QCalendar cal) const
 Returns the day of the year (1 for the first day) for this date.
 
int daysInMonth (QCalendar cal) const
 Returns the number of days in the month for this date.
 
int daysInYear (QCalendar cal) const
 Returns the number of days in the year for this date.
 
QDateTime startOfDay (const QTimeZone &zone) const
 
QDateTime endOfDay (const QTimeZone &zone) const
 
QDateTime startOfDay () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QDateTime endOfDay () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
bool setDate (int year, int month, int day)
 
bool setDate (int year, int month, int day, QCalendar cal)
 
void getDate (int *year, int *month, int *day) const
 
QDate addDays (qint64 days) const
 Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
 
QDate addMonths (int months) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QDate addYears (int years) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QDate addMonths (int months, QCalendar cal) const
 Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).
 
QDate addYears (int years, QCalendar cal) const
 Returns a QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative).
 
qint64 daysTo (QDate d) const
 Returns the number of days from this date to d (which is negative if d is earlier than this date).
 
constexpr qint64 toJulianDay () const
 Converts the date to a Julian day.
 

Static Public Member Functions

static QDate currentDate ()
 Returns the system clock's current date.
 
static bool isValid (int y, int m, int d)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the specified date (year, month, and day) is valid in the Gregorian calendar; otherwise returns false.
 
static bool isLeapYear (int year)
 Returns true if the specified year is a leap year in the Gregorian calendar; otherwise returns false.
 
static constexpr QDate fromJulianDay (qint64 jd_)
 Converts the Julian day jd to a QDate.
 

Friends

class QDateTime
 
class QDateTimeParser
 
class QDateTimePrivate
 
constexpr bool comparesEqual (const QDate &lhs, const QDate &rhs) noexcept
 
constexpr Qt::strong_ordering compareThreeWay (const QDate &lhs, const QDate &rhs) noexcept
 
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, QDate)
 Writes the date to stream out.
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QDate &)
 Reads a date from stream in into the date.
 

Detailed Description

\inmodule QtCore \reentrant

The QDate class provides date functions.

\compares strong \compareswith strong std::chrono::year_month_day std::chrono::year_month_day_last \ std::chrono::year_month_weekday std::chrono::year_month_weekday_last These comparison operators are only available when using C++20. \endcompareswith

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(). The fromString() function returns a QDate given a string and a date format which is used to interpret the date within the string.

The year(), month(), and 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() and 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() and addYears(). The daysTo() function returns the number of days between two dates.

The daysInMonth() and 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.

Definition at line 28 of file qdatetime.h.

Constructor & Destructor Documentation

◆ QDate() [1/3]

QDate::QDate ( )
inlineconstexpr

Constructs a null date.

Null dates are invalid.

See also
isNull(), isValid()

Definition at line 32 of file qdatetime.h.

◆ QDate() [2/3]

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() returns false.

Warning
Years 1 to 99 are interpreted as is. Year 0 is invalid.
See also
isValid(), QCalendar::dateFromParts()

Definition at line 442 of file qdatetime.cpp.

◆ QDate() [3/3]

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

Definition at line 449 of file qdatetime.cpp.

Member Function Documentation

◆ addDays()

QDate QDate::addDays ( qint64 ndays) const
nodiscard

Returns a 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.

See also
addMonths(), addYears(), daysTo()

Definition at line 1399 of file qdatetime.cpp.

◆ addMonths() [1/2]

QDate QDate::addMonths ( int months) const
nodiscard

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1477 of file qdatetime.cpp.

◆ addMonths() [2/2]

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

Returns a 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.

Note
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.
See also
addDays(), addYears()

Definition at line 1445 of file qdatetime.cpp.

◆ addYears() [1/2]

QDate QDate::addYears ( int years) const
nodiscard

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1542 of file qdatetime.cpp.

◆ addYears() [2/2]

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

Returns a 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.

Note
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).
See also
addDays(), addMonths()

Definition at line 1519 of file qdatetime.cpp.

◆ currentDate()

static QDate QDate::currentDate ( )
static

Returns the system clock's current date.

See also
QTime::currentTime(), QDateTime::currentDateTime()

◆ day() [1/2]

int QDate::day ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 642 of file qdatetime.cpp.

◆ day() [2/2]

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.

See also
year(), month(), dayOfWeek(), QCalendar::partsFromDate()

Definition at line 628 of file qdatetime.cpp.

◆ dayOfWeek() [1/2]

int QDate::dayOfWeek ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 674 of file qdatetime.cpp.

◆ dayOfWeek() [2/2]

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.

See also
day(), dayOfYear(), QCalendar::dayOfWeek(), Qt::DayOfWeek

Definition at line 662 of file qdatetime.cpp.

◆ dayOfYear() [1/2]

int QDate::dayOfYear ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 702 of file qdatetime.cpp.

◆ dayOfYear() [2/2]

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.

See also
day(), dayOfWeek(), QCalendar::daysInYear()

Definition at line 688 of file qdatetime.cpp.

◆ daysInMonth() [1/2]

int QDate::daysInMonth ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 735 of file qdatetime.cpp.

◆ daysInMonth() [2/2]

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.

See also
day(), daysInYear(), QCalendar::daysInMonth(), QCalendar::maximumDaysInMonth(), QCalendar::minimumDaysInMonth()

Definition at line 721 of file qdatetime.cpp.

◆ daysInYear() [1/2]

int QDate::daysInYear ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 766 of file qdatetime.cpp.

◆ daysInYear() [2/2]

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.

See also
day(), daysInMonth(), QCalendar::daysInYear(), QCalendar::maximumMonthsInYear()

Definition at line 754 of file qdatetime.cpp.

◆ daysTo()

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.

Example:

See also
addDays()

Definition at line 1573 of file qdatetime.cpp.

◆ endOfDay() [1/2]

QDateTime QDate::endOfDay ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.5

Definition at line 1127 of file qdatetime.cpp.

◆ endOfDay() [2/2]

QDateTime QDate::endOfDay ( const QTimeZone & zone) const
Since
5.14

Returns the end-moment of the day.

When a day ends depends on a how time is described: each day starts and ends earlier for those in time-zones further west and later for those in time-zones further east. The time representation to use can be specified by an optional time zone. The default time representation is the system's local time.

Usually, the end of the day is one millisecond before the midnight, 24:00: however, if a time-zone transition causes the given date to skip over that moment (e.g. a DST spring-forward skipping over 23:00 and the following hour), the actual latest time in the day is returned. This can only arise when the time representation is a time-zone or local time.

When zone has a timeSpec() of Qt::OffsetFromUTC or Qt::UTC, the time representation has no transitions so the end of the day is QTime(23, 59, 59, 999).

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 an invalid time-zone as zone will also produce an invalid result, as shall dates that end outside the range representable by QDateTime.

See also
startOfDay()

Definition at line 1097 of file qdatetime.cpp.

◆ fromJulianDay()

static constexpr QDate QDate::fromJulianDay ( qint64 jd)
inlinestaticconstexpr

Converts the Julian day jd to a QDate.

See also
toJulianDay()

Definition at line 170 of file qdatetime.h.

◆ getDate()

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

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.

Note
In Qt versions prior to 5.7, this function is marked as non-{const}.
See also
year(), month(), day(), isValid(), QCalendar::partsFromDate()

Definition at line 1374 of file qdatetime.cpp.

◆ isLeapYear()

bool QDate::isLeapYear ( int year)
static

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

See also
QCalendar::isLeapYear()

Definition at line 1928 of file qdatetime.cpp.

◆ isNull()

bool QDate::isNull ( ) const
inlineconstexpr

Returns true if the date is null; otherwise returns false.

A null date is invalid.

Note
The behavior of this function is equivalent to isValid().
See also
isValid()

Definition at line 71 of file qdatetime.h.

◆ isValid() [1/2]

bool QDate::isValid ( ) const
inlineconstexpr

Returns true if this date is valid; otherwise returns false.

See also
isNull(), QCalendar::isDateValid()

Definition at line 72 of file qdatetime.h.

◆ isValid() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the specified date (year, month, and day) is valid in the Gregorian calendar; otherwise returns false.

Example:

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)
See also
isNull(), setDate(), QCalendar::isDateValid()

Definition at line 1914 of file qdatetime.cpp.

◆ month() [1/2]

int QDate::month ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 609 of file qdatetime.cpp.

◆ month() [2/2]

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:

\list

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

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

See also
year(), day(), QCalendar::partsFromDate()

Definition at line 595 of file qdatetime.cpp.

◆ setDate() [1/2]

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

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.

See also
isValid(), QCalendar::dateFromParts()

Definition at line 1338 of file qdatetime.cpp.

◆ setDate() [2/2]

bool QDate::setDate ( int year,
int month,
int day,
QCalendar cal )
Since
5.14

Sets this to represent the date, in the given calendar cal, 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.

See also
isValid(), QCalendar::dateFromParts()

Definition at line 1356 of file qdatetime.cpp.

◆ startOfDay() [1/2]

QDateTime QDate::startOfDay ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.5

Definition at line 972 of file qdatetime.cpp.

◆ startOfDay() [2/2]

QDateTime QDate::startOfDay ( const QTimeZone & zone) const
Since
5.14

Returns the start-moment of the day.

When a day starts depends on a how time is described: each day starts and ends earlier for those in time-zones further west and later for those in time-zones further east. The time representation to use can be specified by an optional time zone. The default time representation is the system's local time.

Usually, the start of the day is midnight, 00:00: however, if a time-zone transition causes the given date to skip over that midnight (e.g. a DST spring-forward skipping over the first hour of the day day), the actual earliest time in the day is returned. This can only arise when the time representation is a time-zone or local time.

When zone has a timeSpec() of is Qt::OffsetFromUTC or Qt::UTC, the time representation has no transitions so the start of the day is QTime(0, 0).

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 an invalid time-zone as zone will also produce an invalid result, as shall dates that start outside the range representable by QDateTime.

See also
endOfDay()

Definition at line 941 of file qdatetime.cpp.

◆ toJulianDay()

qint64 QDate::toJulianDay ( ) const
inlineconstexpr

Converts the date to a Julian day.

See also
fromJulianDay()

Definition at line 172 of file qdatetime.h.

◆ weekNumber()

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 is not \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.

Note
*{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.
See also
isValid()

Definition at line 790 of file qdatetime.cpp.

◆ year() [1/2]

int QDate::year ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 557 of file qdatetime.cpp.

◆ year() [2/2]

int QDate::year ( QCalendar cal) const

Returns the year of this date.

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.

See also
month(), day(), QCalendar::hasYearZero(), QCalendar::isProleptic(), QCalendar::partsFromDate()

Definition at line 543 of file qdatetime.cpp.

Friends And Related Symbol Documentation

◆ comparesEqual

bool comparesEqual ( const QDate & lhs,
const QDate & rhs )
friend

Definition at line 207 of file qdatetime.h.

◆ compareThreeWay

Qt::strong_ordering compareThreeWay ( const QDate & lhs,
const QDate & rhs )
friend

Definition at line 210 of file qdatetime.h.

◆ operator<<

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

Writes the date to stream out.

See also
{Serializing Qt Data Types}

Definition at line 6005 of file qdatetime.cpp.

◆ operator>>

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

Reads a date from stream in into the date.

See also
{Serializing Qt Data Types}

Definition at line 6021 of file qdatetime.cpp.

◆ QDateTime

friend class QDateTime
friend

Definition at line 203 of file qdatetime.h.

◆ QDateTimeParser

friend class QDateTimeParser
friend

Definition at line 204 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 205 of file qdatetime.h.


The documentation for this class was generated from the following files: