6.0.0-rc4Source

QDate Class

The QDate class provides date functions. More…

Header#include <QDate>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QDate()
QDate(int y, int m, int d)
QDate(int y, int m, int d, QCalendar cal)
boolisNull() const
boolisValid() const
intyear() const
intmonth() const
intday() const
intdayOfWeek() const
intdayOfYear() const
intdaysInMonth() const
intdaysInYear() const
intyear(QCalendar cal) const
intmonth(QCalendar cal) const
intday(QCalendar cal) const
intdayOfYear(QCalendar cal) const
intdaysInMonth(QCalendar cal) const
intdaysInYear(QCalendar cal) const
boolsetDate(int year, int month, int day, QCalendar cal)
QDateaddMonths(int months, QCalendar cal) const
QDateaddYears(int years, QCalendar cal) const
intweekNumber(int *yearNumber = nullptr) const
boolsetDate(int year, int month, int day)
voidgetDate(int *year, int *month, int *day) const
QDateaddDays(qint64 days) const
QDateaddMonths(int months) const
QDateaddYears(int years) const
qint64daysTo(QDate d) const
QDateTimestartOfDay(Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const
QDateTimeendOfDay(Qt::TimeSpec spec = Qt::LocalTime, int offsetSeconds = 0) const
QDateTimestartOfDay(const QTimeZone &zone) const
QDateTimeendOfDay(const QTimeZone &zone) const
qint64toJulianDay() const
QStringtoString(Qt::DateFormat format = Qt::TextDate) const
QStringtoString(const QString &format) const

Static Public Members

QDatecurrentDate()
QDatefromString(const QString &string, Qt::DateFormat format = Qt::TextDate)
QDatefromString(const QString &string, const QString &format)
boolisValid(int y, int m, int d)
boolisLeapYear(int year)
QDatefromJulianDay(qint64 julianDay)

Detailed Description

A QDate represents a calendar date in the proleptic Gregorian calendar, as a year, month and day. Year 0 does not exist: the year before 1 is -1. A default-constructed QDate is null and invalid; so is a QDate built from an impossible date such as February 30.

QDate d(2026, 11, 7);
d.dayOfWeek();            // 6, Saturday
d.addMonths(3).toString(Qt::ISODate); // "2027-02-07"

Text formats use English month and day names, since Nt has no QLocale.

See also QTime and QDateTime.

Differences from Qt

There is no QCalendar support, so every function uses the Gregorian calendar, and there are no startOfDay(), endOfDay() or currentDate() overloads that take a QTimeZone, longMonthName(), shortMonthName(), longDayName() or shortDayName(). Text formats use C-locale English names. The format string passed to toString() and fromString() supports the d, M and y tokens, and single-quoted literal text.

Member Function Documentation

[constexpr] QDate::QDate()

Constructs a null date. A null date is invalid.

See also isNull() and isValid().

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

Constructs a date with year y, month m and day d. If the date is not valid, the QDate is null.

See also isValid() and setDate().

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

This is an overloaded function.

Constructs the date of day d of month m of year y in the calendar cal. The date is null if it does not exist.

This function was introduced in 5.14.

QDate QDate::addDays(qint64 days) const

Returns a QDate that is days days after this date, or before it if days is negative. Returns a null QDate if this date is invalid or the result is out of range.

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

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

This is an overloaded function.

Returns the date months months later (earlier if negative) in the calendar cal. If the day does not exist in the new month, the last day of that month is used.

This function was introduced in 5.14.

QDate QDate::addMonths(int months) const

Returns a QDate that is months months after this date, or before it if months is negative. If the resulting month is too short for the day, the day is clamped to its last day. Returns a null QDate if this date is invalid.

See also addDays() and addYears().

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

This is an overloaded function.

Returns the date years years later (earlier if negative) in the calendar cal, skipping the nonexistent year zero. If the day does not exist in the new year, the last day of the month is used.

This function was introduced in 5.14.

QDate QDate::addYears(int years) const

Returns a QDate that is years years after this date, or before it if years is negative. 29 February is clamped to 28 February if the resulting year is not a leap year. Returns a null QDate if this date is invalid.

See also addDays() and addMonths().

[static] QDate QDate::currentDate()

Returns the current date, as reported by the system clock in local time.

See also QDateTime::currentDateTime() and QTime::currentTime().

int QDate::day() const

Returns the day of the month, from 1 to 31, or 0 if the date is invalid.

See also year(), month() and dayOfWeek().

int QDate::day(QCalendar cal) const

This is an overloaded function.

Returns the day of the month of the date in the calendar cal, or 0.

This function was introduced in 5.14.

int QDate::dayOfWeek() const

Returns the day of the week, from 1 (Monday) to 7 (Sunday), or 0 if the date is invalid.

See also day() and dayOfYear().

int QDate::dayOfYear() const

Returns the day of the year, from 1 for 1 January, or 0 if the date is invalid.

See also day() and daysInYear().

int QDate::dayOfYear(QCalendar cal) const

This is an overloaded function.

Returns the day of the year of the date in the calendar cal, from 1, or 0.

This function was introduced in 5.14.

int QDate::daysInMonth() const

Returns the number of days in the date's month, from 28 to 31, or 0 if the date is invalid.

See also daysInYear().

int QDate::daysInMonth(QCalendar cal) const

This is an overloaded function.

Returns the number of days in the month of the date in the calendar cal, or 0.

This function was introduced in 5.14.

int QDate::daysInYear() const

Returns the number of days in the date's year, 365 or 366, or 0 if the date is invalid.

See also daysInMonth() and isLeapYear().

int QDate::daysInYear(QCalendar cal) const

This is an overloaded function.

Returns the number of days in the year of the date in the calendar cal, or 0.

This function was introduced in 5.14.

qint64 QDate::daysTo(QDate d) const

Returns the number of days from this date to d, which is negative if d is earlier. Returns 0 if either date is invalid.

See also addDays().

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

Returns the last moment of this date, one millisecond before the next day starts, in the time specification spec, with offsetSeconds as the offset when spec is Qt::OffsetFromUTC. Returns an invalid QDateTime if the date is invalid.

This function was introduced in 5.14.

See also startOfDay().

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

Returns the last moment of this date in zone, one millisecond before the next day starts.

This function was introduced in 5.14.

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

Returns the QDate for the Julian day julianDay. Returns a null QDate if julianDay is outside the range that Qt supports.

See also toJulianDay().

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

Parses string as a date in the given format and returns it. Returns a null QDate if string does not match or is not a valid date. For ISODate and ISODateWithMs the text must start with yyyy-MM-dd; for RFC2822Date a leading weekday is ignored. TextDate expects ddd MMM d yyyy.

See also toString().

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

This is an overloaded function.

Parses string using format, which uses the same expressions as toString(const QString &) const, and returns the date. Returns a null QDate if string does not match format or is not a valid date. An absent year is taken as 1900, and a two-digit year is taken as 19xx.

See also toString().

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

Stores the year, month and day of the date in year, month and day. A pointer may be nullptr to skip that value. All three are set to 0 if the date is invalid.

See also setDate().

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

Returns true if year is a leap year in the Gregorian calendar; otherwise returns false. Year 1 BC is year -1 and is a leap year.

See also daysInYear().

[constexpr] bool QDate::isNull() const

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

See also isValid().

[constexpr] bool QDate::isValid() const

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

See also isNull().

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

This is an overloaded function.

Returns true if the specified date (y, m, d) is valid; otherwise returns false. A date is valid if the year is not 0, the month is 1 to 12, and the day exists in that month.

See also setDate().

int QDate::month() const

Returns the month of the date, from 1 (January) to 12 (December), or 0 if the date is invalid.

See also year() and day().

int QDate::month(QCalendar cal) const

This is an overloaded function.

Returns the month of the date in the calendar cal, or 0.

This function was introduced in 5.14.

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

This is an overloaded function.

Sets the date to day day of month month of year year in the calendar cal and returns true, or makes it null and returns false if that date does not exist.

This function was introduced in 5.14.

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

Sets the date to year, month and day. Returns true if the date is valid; otherwise makes the QDate null and returns false.

See also isValid() and getDate().

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

Returns the first moment of this date in the time specification spec, with offsetSeconds as the offset when spec is Qt::OffsetFromUTC. The result is midnight, unless the date starts later in local time because a daylight-saving transition skips midnight; then it is the first minute of the day that exists. Returns an invalid QDateTime if the date is invalid.

This function was introduced in 5.14.

See also endOfDay().

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

Returns the first moment of this date in zone. If a daylight-saving transition skips midnight, this is the first moment after the gap.

This function was introduced in 5.14.

[constexpr] qint64 QDate::toJulianDay() const

Returns the date as a Julian day number, or the minimum qint64 value if the date is invalid.

See also fromJulianDay().

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

Returns the date as text in the given format, or an empty string if the date is invalid. TextDate gives ddd MMM d yyyy, ISODate and ISODateWithMs give yyyy-MM-dd (empty for years outside 0 to 9999), and RFC2822Date gives dd MMM yyyy. Names are English.

See also fromString().

QString QDate::toString(const QString &format) const

This is an overloaded function.

Returns the date as text using format, or an empty string if the date is invalid. The expressions are d and dd (day as 1 to 31 or 01 to 31), ddd and dddd (abbreviated and full English day name), M and MM (month as 1 to 12 or 01 to 12), MMM and MMMM (abbreviated and full English month name), yy (two-digit year) and yyyy (four-digit year, with a leading minus sign if negative). Text in single quotes is copied as is, and two single quotes give one quote. Other characters are copied unchanged.

See also fromString().

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

Returns the ISO 8601 week number, from 1 to 53, or 0 if the date is invalid. Weeks start on Monday, and week 1 is the week with the year's first Thursday. If yearNumber is not nullptr, it is set to the year that the week belongs to, which can differ from year() near the start and end of the year.

See also dayOfYear().

int QDate::year() const

Returns the year of the date, or 0 if the date is invalid. There is no year 0, so the year before 1 is -1.

See also month() and day().

int QDate::year(QCalendar cal) const

This is an overloaded function.

Returns the year of the date in the calendar cal, or 0 if it is invalid.

This function was introduced in 5.14.