QtBase  v6.3.1
Public Member Functions | Static Public Member Functions | Friends | Related Functions | List of all members
QDate Class Reference

The QDate class provides date functions. More...

#include <qdatetime.h>

Public Member Functions

constexpr QDate ()
 
 QDate (int y, int m, int d)
 
 QDate (int y, int m, int d, QCalendar cal)
 
constexpr bool isNull () const
 
constexpr bool isValid () const
 
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
 
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 (Qt::TimeSpec spec=Qt::LocalTime, int offsetSeconds=0) const
 
QDateTime endOfDay (Qt::TimeSpec spec=Qt::LocalTime, int offsetSeconds=0) 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
 
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
 
constexpr qint64 toJulianDay () const
 

Static Public Member Functions

static QDate currentDate ()
 
static bool isValid (int y, int m, int d)
 
static bool isLeapYear (int year)
 
static constexpr QDate fromJulianDay (qint64 jd_)
 

Friends

class QDateTime
 
class QDateTimePrivate
 
constexpr friend bool operator== (QDate lhs, QDate rhs)
 
constexpr friend bool operator!= (QDate lhs, QDate rhs)
 
constexpr friend bool operator< (QDate lhs, QDate rhs)
 
constexpr friend bool operator<= (QDate lhs, QDate rhs)
 
constexpr friend bool operator> (QDate lhs, QDate rhs)
 
constexpr friend bool operator>= (QDate lhs, QDate rhs)
 
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, QDate)
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QDate &)
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, QDate date)
 
QDataStreamoperator>> (QDataStream &in, QDate &date)
 

Detailed Description

The QDate class provides date functions.

\inmodule QtCore \reentrant

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 63 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 67 of file qdatetime.h.

Here is the caller graph for this function:

◆ 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 438 of file qdatetime.cpp.

Here is the call graph for this function:

◆ QDate() [3/3]

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

Definition at line 444 of file qdatetime.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ addDays()

QDate QDate::addDays ( qint64  ndays) const

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 1226 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addMonths() [1/2]

QDate QDate::addMonths ( int  nmonths) 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 1282 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addMonths() [2/2]

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

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 1250 of file qdatetime.cpp.

Here is the call graph for this function:

◆ addYears() [1/2]

QDate QDate::addYears ( int  nyears) 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 1347 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addYears() [2/2]

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

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 1324 of file qdatetime.cpp.

Here is the call graph for this function:

◆ currentDate()

QDate::currentDate ( )
static

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

See also
QTime::currentTime(), QDateTime::currentDateTime()
Here is the caller graph for this function:

◆ 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 589 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 575 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 621 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 609 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 649 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 635 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 683 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 669 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 714 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 702 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 1378 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ endOfDay()

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

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).

The 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.

See also
startOfDay()

Definition at line 967 of file qdatetime.cpp.

Here is the caller graph for this function:

◆ fromJulianDay()

static QDate QDate::fromJulianDay ( qint64  jd)
inlinestaticconstexpr

Converts the Julian day jd to a QDate.

See also
toJulianDay()

Definition at line 137 of file qdatetime.h.

Here is the caller graph for this function:

◆ 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 1201 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 1643 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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.

Here is the caller graph for this function:

◆ 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.

Here is the caller graph for this function:

◆ 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:

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

Definition at line 1629 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 556 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 542 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 1163 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 1183 of file qdatetime.cpp.

Here is the call graph for this function:

◆ startOfDay()

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

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).

The 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.

See also
endOfDay()

Definition at line 840 of file qdatetime.cpp.

Here is the caller graph for this function:

◆ toJulianDay()

int QDate::toJulianDay ( ) const
inlineconstexpr

Converts the date to a Julian day.

See also
fromJulianDay()

Definition at line 139 of file qdatetime.h.

Here is the caller graph for this function:

◆ 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 738 of file qdatetime.cpp.

Here is the call graph for this function:

◆ 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 504 of file qdatetime.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 490 of file qdatetime.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator!=

bool QDate::operator!= ( QDate  lhs,
QDate  rhs 
)
friend

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

See also
operator==()

Definition at line 153 of file qdatetime.h.

◆ operator<

bool QDate::operator< ( QDate  lhs,
QDate  rhs 
)
friend

Returns true if lhs is earlier than rhs; otherwise returns false.

Definition at line 154 of file qdatetime.h.

◆ operator<< [1/2]

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

Writes the date to stream out.

See also
{Serializing Qt Data Types}

Definition at line 5333 of file qdatetime.cpp.

◆ operator<<() [2/2]

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

Writes the date to stream out.

See also
{Serializing Qt Data Types}

Definition at line 5333 of file qdatetime.cpp.

◆ operator<=

bool QDate::operator<= ( QDate  lhs,
QDate  rhs 
)
friend

Returns true if lhs is earlier than or equal to rhs; otherwise returns false.

Definition at line 155 of file qdatetime.h.

◆ operator==

bool QDate::operator== ( QDate  lhs,
QDate  rhs 
)
friend

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

Definition at line 152 of file qdatetime.h.

◆ operator>

bool QDate::operator> ( QDate  lhs,
QDate  rhs 
)
friend

Returns true if lhs is later than rhs; otherwise returns false.

Definition at line 156 of file qdatetime.h.

◆ operator>=

bool QDate::operator>= ( QDate  lhs,
QDate  rhs 
)
friend

Returns true if lhs is later than or equal to rhs; otherwise returns false.

Definition at line 157 of file qdatetime.h.

◆ operator>> [1/2]

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 5349 of file qdatetime.cpp.

◆ operator>>() [2/2]

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

Reads a date from stream in into the date.

See also
{Serializing Qt Data Types}

Definition at line 5349 of file qdatetime.cpp.

◆ QDateTime

friend class QDateTime
friend

Definition at line 149 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 150 of file qdatetime.h.


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