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 Types

using difference_type = qint64

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
int month () const
int day () const
int dayOfWeek () const
int dayOfYear () const
int daysInMonth () const
int daysInYear () const
int weekNumber (int *yearNum=nullptr) const
 Returns the ISO 8601 week number (1 to 53).
int year (QCalendar cal) const
int month (QCalendar cal) const
int day (QCalendar cal) const
int dayOfWeek (QCalendar cal) const
int dayOfYear (QCalendar cal) const
int daysInMonth (QCalendar cal) const
int daysInYear (QCalendar cal) const
QDateTime startOfDay (const QTimeZone &zone) const
QDateTime endOfDay (const QTimeZone &zone) const
QDateTime startOfDay () const
QDateTime endOfDay () const
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
QDate addYears (int years) const
QDate addMonths (int months, QCalendar cal) const
QDate addYears (int years, QCalendar cal) const
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)
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
QDateoperator++ (QDate &date) noexcept
QDateoperator-- (QDate &date) noexcept
QDate operator++ (QDate &date, int) noexcept
QDate operator-- (QDate &date, int) 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.

(Note that these are not member symbols.)

 dayOfYear
 startOfDay
 endOfDay
 dayOfYear
 startOfDay
 endOfDay

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.

Member Typedef Documentation

◆ difference_type

Definition at line 32 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 34 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 448 of file qdatetime.cpp.

◆ QDate() [3/3]

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

Definition at line 455 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 1423 of file qdatetime.cpp.

◆ addMonths() [1/2]

QDate QDate::addMonths ( int months) const
nodiscard

Definition at line 1502 of file qdatetime.cpp.

◆ addMonths() [2/2]

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

Definition at line 1470 of file qdatetime.cpp.

◆ addYears() [1/2]

QDate QDate::addYears ( int years) const
nodiscard

Definition at line 1569 of file qdatetime.cpp.

◆ addYears() [2/2]

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

Definition at line 1546 of file qdatetime.cpp.

◆ currentDate()

QDate QDate::currentDate ( )
static

Returns the system clock's current date.

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

◆ day() [1/2]

int QDate::day ( ) const

Definition at line 655 of file qdatetime.cpp.

◆ day() [2/2]

int QDate::day ( QCalendar cal) const

Definition at line 641 of file qdatetime.cpp.

◆ dayOfWeek() [1/2]

int QDate::dayOfWeek ( ) const

Definition at line 689 of file qdatetime.cpp.

◆ dayOfWeek() [2/2]

int QDate::dayOfWeek ( QCalendar cal) const

Definition at line 677 of file qdatetime.cpp.

◆ dayOfYear() [1/2]

int QDate::dayOfYear ( ) const

Definition at line 719 of file qdatetime.cpp.

◆ dayOfYear() [2/2]

int QDate::dayOfYear ( QCalendar cal) const

Definition at line 705 of file qdatetime.cpp.

◆ daysInMonth() [1/2]

int QDate::daysInMonth ( ) const

Definition at line 754 of file qdatetime.cpp.

◆ daysInMonth() [2/2]

int QDate::daysInMonth ( QCalendar cal) const

Definition at line 740 of file qdatetime.cpp.

◆ daysInYear() [1/2]

int QDate::daysInYear ( ) const

Definition at line 787 of file qdatetime.cpp.

◆ daysInYear() [2/2]

int QDate::daysInYear ( QCalendar cal) const

Definition at line 775 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 1600 of file qdatetime.cpp.

◆ endOfDay() [1/2]

QDateTime QDate::endOfDay ( ) const

Definition at line 1150 of file qdatetime.cpp.

◆ endOfDay() [2/2]

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

Definition at line 1120 of file qdatetime.cpp.

◆ fromJulianDay()

constexpr QDate QDate::fromJulianDay ( qint64 jd)
inlinestaticconstexpr

Converts the Julian day jd to a QDate.

See also
toJulianDay()

Definition at line 172 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 1398 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 1957 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 73 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 74 of file qdatetime.h.

◆ isValid() [2/2]

bool QDate::isValid ( int y,
int m,
int d )
static

Definition at line 1943 of file qdatetime.cpp.

◆ month() [1/2]

int QDate::month ( ) const

Definition at line 620 of file qdatetime.cpp.

◆ month() [2/2]

int QDate::month ( QCalendar cal) const

Definition at line 606 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 1362 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 1380 of file qdatetime.cpp.

◆ startOfDay() [1/2]

QDateTime QDate::startOfDay ( ) const

Definition at line 994 of file qdatetime.cpp.

◆ startOfDay() [2/2]

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

Definition at line 963 of file qdatetime.cpp.

◆ toJulianDay()

qint64 QDate::toJulianDay ( ) const
inlineconstexpr

Converts the date to a Julian day.

See also
fromJulianDay()

Definition at line 174 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 811 of file qdatetime.cpp.

◆ year() [1/2]

int QDate::year ( ) const

Definition at line 566 of file qdatetime.cpp.

◆ year() [2/2]

int QDate::year ( QCalendar cal) const

Definition at line 552 of file qdatetime.cpp.

◆ comparesEqual

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

Definition at line 209 of file qdatetime.h.

◆ compareThreeWay

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

Definition at line 212 of file qdatetime.h.

◆ dayOfYear() [1/2]

dayOfYear ( )
related

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

◆ dayOfYear() [2/2]

dayOfYear ( )
related

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

◆ endOfDay() [1/2]

endOfDay ( )
related

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

◆ endOfDay() [2/2]

endOfDay ( )
related

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

◆ operator++ [1/2]

QDate & operator++ ( QDate & date)
friend
Since
6.11

The prefix {++} operator, adds a day to date and returns a reference to the modified date object.

See also
addDays(), operator--()

Definition at line 216 of file qdatetime.h.

◆ operator++ [2/2]

QDate operator++ ( QDate & date,
int  )
friend
Since
6.11

The postfix {++} operator, adds a day to date and returns a copy of date with the previous date.

See also
addDays(), operator--()

Definition at line 228 of file qdatetime.h.

◆ operator-- [1/2]

QDate & operator-- ( QDate & date)
friend
Since
6.11

The prefix {–} operator, subtracts a day from date and returns a reference to the modified date object.

See also
addDays(), operator++()

Definition at line 222 of file qdatetime.h.

◆ operator-- [2/2]

QDate operator-- ( QDate & date,
int  )
friend
Since
6.11

The postfix {–} operator, subtracts a day from date and returns a copy of date with the next date.

See also
addDays(), operator++()

Definition at line 235 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 6112 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 6128 of file qdatetime.cpp.

◆ QDateTime

friend class QDateTime
friend

Definition at line 205 of file qdatetime.h.

◆ QDateTimeParser

friend class QDateTimeParser
friend

Definition at line 206 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 207 of file qdatetime.h.

◆ startOfDay() [1/2]

startOfDay ( )
related

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

◆ startOfDay() [2/2]

startOfDay ( )
related

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

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