QtBase
v6.3.1
|
The QDate class provides date functions. More...
#include <qdatetime.h>
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 QDataStream & | operator<< (QDataStream &, QDate) |
Q_CORE_EXPORT QDataStream & | operator>> (QDataStream &, QDate &) |
Related Functions | |
(Note that these are not member functions.) | |
QDataStream & | operator<< (QDataStream &out, QDate date) |
QDataStream & | operator>> (QDataStream &in, QDate &date) |
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.
|
inlineconstexpr |
Constructs a null date. Null dates are invalid.
Definition at line 67 of file qdatetime.h.
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
.
Definition at line 438 of file qdatetime.cpp.
QDate::QDate | ( | int | y, |
int | m, | ||
int | d, | ||
QCalendar | cal | ||
) |
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.
Definition at line 1226 of file qdatetime.cpp.
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.
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.
Definition at line 1250 of file qdatetime.cpp.
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.
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.
Definition at line 1324 of file qdatetime.cpp.
|
static |
Returns the current date, as reported by the system clock.
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.
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.
Definition at line 575 of file qdatetime.cpp.
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.
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.
Definition at line 609 of file qdatetime.cpp.
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.
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.
Definition at line 635 of file qdatetime.cpp.
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.
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.
Definition at line 669 of file qdatetime.cpp.
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.
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.
Definition at line 702 of file qdatetime.cpp.
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:
Definition at line 1378 of file qdatetime.cpp.
QDateTime QDate::endOfDay | ( | Qt::TimeSpec | spec = Qt::LocalTime , |
int | offsetSeconds = 0 |
||
) | 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).
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.
Definition at line 967 of file qdatetime.cpp.
Converts the Julian day jd to a QDate.
Definition at line 137 of file qdatetime.h.
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.
{const}.Definition at line 1201 of file qdatetime.cpp.
|
static |
Returns true
if the specified year is a leap year in the Gregorian calendar; otherwise returns false
.
Definition at line 1643 of file qdatetime.cpp.
|
inlineconstexpr |
Returns true
if the date is null; otherwise returns false
. A null date is invalid.
Definition at line 71 of file qdatetime.h.
|
inlineconstexpr |
Returns true
if this date is valid; otherwise returns false
.
Definition at line 72 of file qdatetime.h.
|
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:
Definition at line 1629 of file qdatetime.cpp.
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.
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
Returns 0 if the date is invalid. Note that some calendars may have more than 12 months in some years.
Definition at line 542 of file qdatetime.cpp.
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.
Definition at line 1163 of file qdatetime.cpp.
bool QDate::setDate | ( | int | year, |
int | month, | ||
int | day, | ||
QCalendar | cal | ||
) |
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.
Definition at line 1183 of file qdatetime.cpp.
QDateTime QDate::startOfDay | ( | Qt::TimeSpec | spec = Qt::LocalTime , |
int | offsetSeconds = 0 |
||
) | 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).
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.
Definition at line 840 of file qdatetime.cpp.
|
inlineconstexpr |
Converts the date to a Julian day.
Definition at line 139 of file qdatetime.h.
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.
Definition at line 738 of file qdatetime.cpp.
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.
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.
Definition at line 490 of file qdatetime.cpp.
|
friend |
Returns true
if lhs and rhs represent distinct days; otherwise returns false
.
Definition at line 153 of file qdatetime.h.
Returns true
if lhs is earlier than rhs; otherwise returns false
.
Definition at line 154 of file qdatetime.h.
|
friend |
Writes the date to stream out.
Definition at line 5333 of file qdatetime.cpp.
|
related |
Writes the date to stream out.
Definition at line 5333 of file qdatetime.cpp.
Returns true
if lhs is earlier than or equal to rhs; otherwise returns false
.
Definition at line 155 of file qdatetime.h.
Returns true
if lhs and rhs represent the same day, otherwise false
.
Definition at line 152 of file qdatetime.h.
Returns true
if lhs is later than rhs; otherwise returns false
.
Definition at line 156 of file qdatetime.h.
Returns true
if lhs is later than or equal to rhs; otherwise returns false
.
Definition at line 157 of file qdatetime.h.
|
friend |
Reads a date from stream in into the date.
Definition at line 5349 of file qdatetime.cpp.
|
related |
Reads a date from stream in into the date.
Definition at line 5349 of file qdatetime.cpp.
|
friend |
Definition at line 149 of file qdatetime.h.
|
friend |
Definition at line 150 of file qdatetime.h.