![]() |
CopperSpice API
1.8.2
|
The QDate class stores a calendar date which consists of a year, month, and day. More...
Public Types | |
enum | MonthNameType |
Public Methods | |
constexpr | QDate () |
QDate (int y, int m, int d) | |
QDate | addDays (qint64 days) const |
QDate | addMonths (qint64 months) const |
QDate | addYears (qint64 years) const |
int | day () const |
int | dayOfWeek () const |
int | dayOfYear () const |
int | daysInMonth () const |
int | daysInYear () const |
qint64 | daysTo (const QDate &value) const |
void | getDate (int *year, int *month, int *day) |
bool | isNull () const |
bool | isValid () const |
int | month () const |
bool | operator!= (const QDate &value) const |
bool | operator< (const QDate &value) const |
bool | operator<= (const QDate &value) const |
bool | operator== (const QDate &value) const |
bool | operator> (const QDate &value) const |
bool | operator>= (const QDate &value) const |
bool | setDate (int year, int month, int day) |
constexpr qint64 | toJulianDay () const |
QString | toString (const QString &format) const |
QString | toString (Qt::DateFormat format=Qt::TextDate) const |
int | weekNumber (int *yearNumber=nullptr) const |
int | year () const |
Static Public Methods | |
static QDate | currentDate () |
static constexpr QDate | fromJulianDay (qint64 dayNumber) |
static QDate | fromString (const QString &str, const QString &format) |
static QDate | fromString (const QString &str, Qt::DateFormat format=Qt::TextDate) |
static bool | isLeapYear (int year) |
static bool | isValid (int year, int month, int day) |
static QString | longDayName (int weekday, MonthNameType type=DateFormat) |
static QString | longMonthName (int month, MonthNameType type=DateFormat) |
static QString | shortDayName (int weekday, MonthNameType type=DateFormat) |
static QString | shortMonthName (int month, MonthNameType type=DateFormat) |
Friends | |
QDataStream & | operator<< (QDataStream &stream, const QDate &date) |
QDataStream & | operator>> (QDataStream &stream, QDate &date) |
class | QDateTime |
The QDate class stores a calendar date which consists of a year, month, and day. The Gregorian calendar is used for dates after 15 October 1582. Earlier dates use the Julian calendar. This class can retrieve the current date from the system clock. Various methods are provided for comparing and calculating dates.
A QDate object is typically created by specifying the year, month, and day numbers explicitly. QDate interprets two digit years as the years 0 through 99. A QDate can also be constructed with the static method currentDate() which creates a QDate object containing the system clock's date. An explicit date can also be set using setDate(). The fromString() method 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() methods provide access to the year, month, and day numbers. Also, dayOfWeek() and dayOfYear() functions are provided. The same information is provided in textual format by the toString(), shortDayName(), longDayName(), shortMonthName(), and longMonthName() methods.
There are a full set of operators to compare two QDate objects where smaller means earlier and larger means later in time. You can increment (or decrement) a date by a given number of days using addDays(). Similarly you can use addMonths() and addYears(). The daysTo() method returns the number of days between two dates.
The daysInMonth() and daysInYear() methods return how many days there are in this date's month and year, respectively. The isLeapYear() method indicates whether a date is in a leap year.
QDate uses the Gregorian calendar in all locales, beginning on the date 15 October 1582. For dates up to and including 4 October 1582, the Julian calendar is used. This means there is a 10-day gap in the internal calendar between the 4th and the 15th of October 1582. When you use QDateTime for dates in that epoch, the day after 4 October 1582 is 15 October 1582, and the dates in the gap are invalid.
The Julian to Gregorian changeover date used here is the date when the Gregorian calendar was first introduced, by Pope Gregory XIII. That change was not universally accepted and some localities only executed it at a later date (if at all). QDateTime does not take any of these historical facts into account. If an application must support a locale-specific dating system, it must do so on its own, remembering to convert the dates using the Julian day.
There is no year 0. Dates in that year are considered invalid. The year -1 is the year "1 before Christ" or "1 before current era." The day before 0001-01-01 is December 31st, 1 BCE.
The range of valid dates is from January 2nd, 4713 BCE, to sometime in the year 11 million CE. The Julian Day returned by QDate::toJulianDay() is a number in the contiguous range from 1 to overflow, even across QDateTime's "date holes". It is suitable for use in applications that must convert a QDateTime to a date in another calendar system, e.g., Hebrew, Islamic or Chinese.
enum QDate::MonthNameType |
This enum describes the types of the string representation used for the month name.
Constant | Value | Description |
---|---|---|
QDate::DateFormat | 0 | This type of name can be used for date-to-string formatting. |
QDate::StandaloneFormat | 1 | This type is used when you need to enumerate months or weekdays. Usually standalone names are represented in singular forms with capitalized first letter. |
|
inlineconstexpr |
QDate::QDate | ( | int | y, |
int | m, | ||
int | d | ||
) |
|
nodiscard |
Returns a QDate object containing a date days later than the date of this object (or earlier if days is negative).
|
nodiscard |
Returns a QDate object containing a date months later than the date of this object (or earlier if months is negative).
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.
|
nodiscard |
Returns a QDate object containing a date years later than the date of this object (or earlier if years is negative).
|
static |
Returns the current date, as reported by the system clock.
int QDate::day | ( | ) | const |
Returns the day of the month (1 to 31) of this date.
int QDate::dayOfWeek | ( | ) | const |
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
int QDate::dayOfYear | ( | ) | const |
Returns the day of the year (1 to 365 or 366 on leap years) for this date.
int QDate::daysInMonth | ( | ) | const |
Returns the number of days in the month (28 to 31) for this date.
int QDate::daysInYear | ( | ) | const |
Returns the number of days in the year (365 or 366) for this date.
qint64 QDate::daysTo | ( | const QDate & | value | ) | const |
|
inlinestaticconstexpr |
Converts the Julian day dayNumber to a QDate.
Returns the QDate represented by the str, using the format given, or an invalid date if the string can not be parsed.
These expressions may be used for the format:
Expression | Output |
---|---|
d | The day as a number without a leading zero (1 to 31) |
dd | The day as a number with a leading zero (01 to 31) |
ddd | The abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | The long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName(). |
M | The month as a number without a leading zero (1 to 12) |
MM | The month as a number with a leading zero (01 to 12) |
MMM | The abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | The long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | The year as two digit number (00 to 99) |
yyyy | The year as four digit number. If the year is negative, a minus sign is prepended in addition. |
All other input characters will be treated as text. Any sequence of characters that are enclosed in single quotes will also be treated as text and will not be used as an expression.
If the format is not satisfied, an invalid QDate is returned. The expressions that do not expect leading zeroes (d, M) will be greedy. This means that they will use two digits even if this will put them outside the accepted range of values and leaves too few digits for other sections. For example, the following format string could have meant January 30 but the M will grab two digits, resulting in an invalid date:
For any field that is not represented in the format the following defaults are used:
Field | Default value |
---|---|
Year | 1900 |
Month | 1 |
Day | 1 |
The following examples demonstrate the default values:
|
static |
Returns the QDate represented by the str, using the format given, or an invalid date if the string can not be parsed.
For Qt::TextDate it is recommended you use the English short month names (e.g. "Jan"). Although localized month names can also be used they depend on the user's locale settings.
void QDate::getDate | ( | int * | year, |
int * | month, | ||
int * | day | ||
) |
|
static |
Returns true if the specified year is a leap year, otherwise returns false.
|
inline |
|
inline |
Returns true if this date is valid, otherwise returns false.
|
static |
Returns true if the specified year, month, and day are valid, otherwise returns false.
|
static |
Returns the long name of the weekday for the representation specified by type. The day names will be localized according to the system's default locale settings. Returns an empty string if the date is invalid.
The days are enumerated using the following convention:
|
static |
Returns the long name of the month for the representation specified by type. The month names will be localized according to the system's default locale settings. Returns an empty string if the date is invalid.
The months are enumerated using the following convention:
int QDate::month | ( | ) | const |
|
inline |
Returns true if this date is different from value, otherwise returns false.
|
inline |
Returns true if this date is earlier than value, otherwise returns false.
|
inline |
Returns true if this date is earlier than or equal to value, otherwise returns false.
|
inline |
Returns true if this date is equal to value, otherwise returns false.
|
inline |
Returns true if this date is later than value, otherwise returns false.
|
inline |
Returns true if this date is later than or equal to value, otherwise returns false.
bool QDate::setDate | ( | int | year, |
int | month, | ||
int | day | ||
) |
Sets the date's year, month, and day. Returns true if the date is valid, otherwise returns false.
If the specified date is invalid, the QDate object is set to be invalid. Any date before 2 January 4713 B.C. is considered invalid.
|
static |
Returns the short name of the weekday for the representation specified by type. The day names will be localized according to the system's default locale settings. Returns an empty string if the date is invalid.
The days are enumerated using the following convention:
|
static |
Returns the short name of the month for the representation specified by type. The month names will be localized according to the system's default locale settings. Returns an empty string if the date is invalid.
The months are enumerated using the following convention:
|
inlineconstexpr |
Converts the date to a Julian day.
Returns the date as a string. The format parameter determines the format of the result string.
These expressions may be used:
Expression | Output |
---|---|
d | the day as number without a leading zero (1 to 31) |
dd | the day as number with a leading zero (01 to 31) |
ddd | the abbreviated localized day name (e.g. 'Mon' to 'Sun'). Uses QDate::shortDayName(). |
dddd | the long localized day name (e.g. 'Monday' to 'Sunday'). Uses QDate::longDayName(). |
M | the month as number without a leading zero (1 to 12) |
MM | the month as number with a leading zero (01 to 12) |
MMM | the abbreviated localized month name (e.g. 'Jan' to 'Dec'). Uses QDate::shortMonthName(). |
MMMM | the long localized month name (e.g. 'January' to 'December'). Uses QDate::longMonthName(). |
yy | the year as two digit number (00 to 99) |
yyyy | the year as four digit number. If the year is negative, a minus sign is prepended in addition. |
All other input characters will be ignored. Any sequence of characters that are enclosed in singlequotes will be treated as text and not be used as an expression. Two consecutive singlequotes ("''") are replaced by a singlequote in the output.
Example format strings (assuming that the QDate is the 20 July 1969):
Format | Result |
---|---|
dd.MM.yyyy | 20.07.1969 |
ddd MMMM d yy | Sun July 20 69 |
'The day is' dddd | The day is Sunday |
If the datetime is invalid an empty string will be returned.
QString QDate::toString | ( | Qt::DateFormat | format = Qt::TextDate | ) | const |
Returns the date as a string. The format parameter determines the format of the string.
If the format is Qt::TextDate, the string is formatted in the default way. QDate::shortDayName() and QDate::shortMonthName() are used to generate the string, so the day and month names will be localized names using the default locale from the system. An example of this formatting is "Sat May 20 1995".
If theformat is Qt::ISODate, the string format corresponds to the ISO 8601 extended specification for representations of dates and times, taking the form YYYY-MM-DD, where YYYY is the year, MM is the month of the year (between 01 and 12), and DD is the day of the month between 01 and 31.
If the format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale settings of the system. Identical to calling QLocale::system().toString(date, QLocale::ShortFormat) or QLocale::system().toString(date, QLocale::LongFormat).
If the format is Qt::DefaultLocaleShortDate or Qt::DefaultLocaleLongDate, the string format depends on the default application locale. This is the locale set with QLocale::setDefault(), or the system locale if no default locale has been set. Identical to calling QLocale().toString(date, QLocale::ShortFormat) or QLocale().toString(date, QLocale::LongFormat).
If the date is invalid an empty string will be returned.
int QDate::weekNumber | ( | int * | yearNumber = nullptr | ) | const |
Returns the week number (1 to 53), and stores the year in yearNumber unless yearNumber is null (the default). Returns 0 if the date is invalid.
In accordance with ISO 8601, weeks start on Monday and the first Thursday of a year is always in week 1 of that year. Most years have 52 weeks, but some have 53.
The value for 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.
int QDate::year | ( | ) | const |
|
friend |
Writes the given date to the stream. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.
|
friend |
Reads from the stream into the given date. Returns a reference to the stream.
Refer to Serializing Data Types for additional information.