Qt 4.8
Public Types | Public Functions | Static Public Functions | Properties | Friends | List of all members
QDate Class Reference

The QDate class provides date functions. More...

#include <qdatetime.h>

Public Types

enum  MonthNameType { DateFormat = 0, StandaloneFormat }
 This enum describes the types of the string representation used for the month name. More...
 

Public Functions

QDate addDays (int days) const
 Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative). More...
 
QDate addMonths (int months) const
 Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative). More...
 
QDate addYears (int years) const
 Returns a QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative). More...
 
int day () const
 Returns the day of the month (1 to 31) of this date. More...
 
int dayOfWeek () const
 Returns the weekday (1 = Monday to 7 = Sunday) for this date. More...
 
int dayOfYear () const
 Returns the day of the year (1 to 365 or 366 on leap years) for this date. More...
 
int daysInMonth () const
 Returns the number of days in the month (28 to 31) for this date. More...
 
int daysInYear () const
 Returns the number of days in the year (365 or 366) for this date. More...
 
int daysTo (const QDate &) const
 Returns the number of days from this date to d (which is negative if d is earlier than this date). More...
 
void getDate (int *year, int *month, int *day)
 
bool isNull () const
 Returns true if the date is null; otherwise returns false. More...
 
bool isValid () const
 Returns true if this date is valid; otherwise returns false. More...
 
int month () const
 Returns the number corresponding to the month of this date, using the following convention: More...
 
bool operator!= (const QDate &other) const
 Returns true if this date is different from d; otherwise returns false. More...
 
bool operator< (const QDate &other) const
 Returns true if this date is earlier than d; otherwise returns false. More...
 
bool operator<= (const QDate &other) const
 Returns true if this date is earlier than or equal to d; otherwise returns false. More...
 
bool operator== (const QDate &other) const
 Returns true if this date is equal to d; otherwise returns false. More...
 
bool operator> (const QDate &other) const
 Returns true if this date is later than d; otherwise returns false. More...
 
bool operator>= (const QDate &other) const
 Returns true if this date is later than or equal to d; otherwise returns false. More...
 
 QDate ()
 Constructs a null date. More...
 
 QDate (int y, int m, int d)
 Constructs a date with year y, month m and day d. More...
 
bool setDate (int year, int month, int day)
 Sets the date's year, month, and day. More...
 
bool setYMD (int y, int m, int d)
 Sets the date's year y, month m, and day d. More...
 
int toJulianDay () const
 Converts the date to a Julian day. More...
 
QString toString (Qt::DateFormat f=Qt::TextDate) const
 Returns the date as a string. More...
 
QString toString (const QString &format) const
 Returns the date as a string. More...
 
int weekNumber (int *yearNum=0) const
 Returns the week number (1 to 53), and stores the year in {yearNumber} unless yearNumber is null (the default). More...
 
int year () const
 Returns the year of this date. More...
 

Static Public Functions

static QDate currentDate ()
 Returns the current date, as reported by the system clock. More...
 
static QDate fromJulianDay (int jd)
 Converts the Julian day jd to a QDate. More...
 
static QDate fromString (const QString &s, Qt::DateFormat f=Qt::TextDate)
 Returns the QDate represented by the string, using the format given, or an invalid date if the string cannot be parsed. More...
 
static QDate fromString (const QString &s, const QString &format)
 Returns the QDate represented by the string, using the format given, or an invalid date if the string cannot be parsed. More...
 
static uint gregorianToJulian (int y, int m, int d)
 This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian(). More...
 
static bool isLeapYear (int year)
 Returns true if the specified year is a leap year; otherwise returns false. More...
 
static bool isValid (int y, int m, int d)
 Returns true if the specified date (year, month, and day) is valid; otherwise returns false. More...
 
static void julianToGregorian (uint jd, int &y, int &m, int &d)
 This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian(). More...
 
static QString longDayName (int weekday)
 Returns the long version of the name of the weekday. More...
 
static QString longDayName (int weekday, MonthNameType type)
 Returns the long name of the weekday for the representation specified by type. More...
 
static QString longMonthName (int month)
 Returns the long version of the name of the month. More...
 
static QString longMonthName (int month, MonthNameType type)
 Returns the long name of the month for the representation specified by type. More...
 
static QString shortDayName (int weekday)
 Returns the short version of the name of the weekday. More...
 
static QString shortDayName (int weekday, MonthNameType type)
 Returns the short name of the weekday for the representation specified by type. More...
 
static QString shortMonthName (int month)
 Returns the short version of the name of the month. More...
 
static QString shortMonthName (int month, MonthNameType type)
 Returns the short name of the month for the representation specified by type. More...
 

Properties

uint jd
 

Friends

Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &, const QDate &)
 Writes the date to stream out. More...
 
Q_CORE_EXPORT QDataStreamoperator>> (QDataStream &, QDate &)
 Reads a date from stream in into the date. More...
 
class QDateTime
 
class QDateTimePrivate
 

Detailed Description

The QDate class provides date functions.

Note
This class or function is reentrant.

A QDate object contains a calendar date, i.e. year, month, and day numbers, in the Gregorian calendar. (see QDate G and J {Use of Gregorian and Julian Calendars} for dates prior to 15 October 1582). It can read the current date from the system clock. It provides functions for comparing dates, and for manipulating dates. For example, it is possible to add and subtract days, months, and years to dates.

A QDate object is typically created either by giving the year, month, and day numbers explicitly. Note that QDate interprets two digit years as is, i.e., years 0 - 99. A QDate can also be constructed with the static function currentDate(), which creates a QDate object containing the system clock's date. 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. Also, dayOfWeek() and dayOfYear() functions are provided. The same information is provided in textual format by the toString(), shortDayName(), longDayName(), shortMonthName(), and longMonthName() functions.

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.

Definition at line 55 of file qdatetime.h.

Enumerations

◆ MonthNameType

This enum describes the types of the string representation used for the month name.

Since
4.5
  • DateFormat This type of name can be used for date-to-string formatting.
  • StandaloneFormat This type is used when you need to enumerate months or weekdays. Usually standalone names are represented in singular forms with capitalized first letter.
Enumerator
DateFormat 
StandaloneFormat 

Definition at line 58 of file qdatetime.h.

Constructors and Destructors

◆ QDate() [1/2]

QDate::QDate ( )
inline

Constructs a null date.

Null dates are invalid.

See also
isNull(), isValid()

Definition at line 63 of file qdatetime.h.

Referenced by addMonths(), addYears(), and fromString().

63 { jd = 0; }
uint jd
Definition: qdatetime.h:137

◆ QDate() [2/2]

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

Constructs a date with year y, month m and day d.

If the specified date is invalid, the date is not set and isValid() returns false. A date before 2 January 4713 B.C. is considered invalid.

Warning
Years 0 to 99 are interpreted as is, i.e., years 0-99.
See also
isValid()

Definition at line 313 of file qdatetime.cpp.

314 {
315  setDate(y, m, d);
316 }
double d
Definition: qnumeric_p.h:62
bool setDate(int year, int month, int day)
Sets the date&#39;s year, month, and day.
Definition: qdatetime.cpp:959

Functions

◆ addDays()

QDate QDate::addDays ( int  ndays) const

Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).

See also
addMonths() addYears() daysTo()

Definition at line 989 of file qdatetime.cpp.

Referenced by QDateTime::addDays(), QPatternist::AbstractDateTime::create(), QCalendarModel::dateForCell(), fromDate(), fromTimeStamp(), QTDSResult::gotoNext(), QCalendarView::moveCursor(), QDateTime::setMSecsSinceEpoch(), and QDateTime::setTime_t().

990 {
991  QDate d;
992  // this is basically "d.jd = jd + ndays" with checks for integer overflow
993  if (ndays >= 0)
994  d.jd = (jd + ndays >= jd) ? jd + ndays : 0;
995  else
996  d.jd = (jd + ndays < jd) ? jd + ndays : 0;
997  return d;
998 }
double d
Definition: qnumeric_p.h:62
The QDate class provides date functions.
Definition: qdatetime.h:55
uint jd
Definition: qdatetime.h:137

◆ addMonths()

QDate QDate::addMonths ( int  nmonths) const

Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).

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.
Warning
QDate has a date hole around the days introducing the Gregorian calendar (the days 5 to 14 October 1582, inclusive, do not exist). If the calculation ends in one of those days, QDate will return either October 4 or October 15.
See also
addDays() addYears()

Definition at line 1016 of file qdatetime.cpp.

Referenced by QCalendarWidgetPrivate::_q_monthChanged(), QCalendarWidgetPrivate::_q_nextMonthClicked(), QCalendarWidgetPrivate::_q_prevMonthClicked(), QDateTime::addMonths(), QCalendarView::moveCursor(), and QCalendarView::wheelEvent().

1017 {
1018  if (!isValid())
1019  return QDate();
1020  if (!nmonths)
1021  return *this;
1022 
1023  int old_y, y, m, d;
1024  getDateFromJulianDay(jd, &y, &m, &d);
1025  old_y = y;
1026 
1027  bool increasing = nmonths > 0;
1028 
1029  while (nmonths != 0) {
1030  if (nmonths < 0 && nmonths + 12 <= 0) {
1031  y--;
1032  nmonths+=12;
1033  } else if (nmonths < 0) {
1034  m+= nmonths;
1035  nmonths = 0;
1036  if (m <= 0) {
1037  --y;
1038  m += 12;
1039  }
1040  } else if (nmonths - 12 >= 0) {
1041  y++;
1042  nmonths -= 12;
1043  } else if (m == 12) {
1044  y++;
1045  m = 0;
1046  } else {
1047  m += nmonths;
1048  nmonths = 0;
1049  if (m > 12) {
1050  ++y;
1051  m -= 12;
1052  }
1053  }
1054  }
1055 
1056  // was there a sign change?
1057  if ((old_y > 0 && y <= 0) ||
1058  (old_y < 0 && y >= 0))
1059  // yes, adjust the date by +1 or -1 years
1060  y += increasing ? +1 : -1;
1061 
1062  // did we end up in the Gregorian/Julian conversion hole?
1063  if (y == 1582 && m == 10 && d > 4 && d < 15)
1064  d = increasing ? 15 : 4;
1065 
1066  return fixedDate(y, m, d);
1067 }
double d
Definition: qnumeric_p.h:62
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
QDate()
Constructs a null date.
Definition: qdatetime.h:63
static QDate fixedDate(int y, int m, int d)
Definition: qdatetime.cpp:96
uint jd
Definition: qdatetime.h:137

◆ addYears()

QDate QDate::addYears ( int  nyears) const

Returns a QDate object containing a date nyears later than the date of this object (or earlier if nyears is negative).

Note
If the ending day/month combination does not exist in the resulting year (i.e., 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 (that is, Feb 28).
See also
addDays(), addMonths()

Definition at line 1081 of file qdatetime.cpp.

Referenced by QCalendarWidgetPrivate::_q_yearEditingFinished(), and QDateTime::addYears().

1082 {
1083  if (!isValid())
1084  return QDate();
1085 
1086  int y, m, d;
1087  getDateFromJulianDay(jd, &y, &m, &d);
1088 
1089  int old_y = y;
1090  y += nyears;
1091 
1092  // was there a sign change?
1093  if ((old_y > 0 && y <= 0) ||
1094  (old_y < 0 && y >= 0))
1095  // yes, adjust the date by +1 or -1 years
1096  y += nyears > 0 ? +1 : -1;
1097 
1098  return fixedDate(y, m, d);
1099 }
double d
Definition: qnumeric_p.h:62
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
QDate()
Constructs a null date.
Definition: qdatetime.h:63
static QDate fixedDate(int y, int m, int d)
Definition: qdatetime.cpp:96
uint jd
Definition: qdatetime.h:137

◆ currentDate()

QDate QDate::currentDate ( )
static

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

If spec is Qt::LocalTime, use the currentDate() overload that takes no parameters instead; otherwise, use QDateTime::currentDateTime().

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

QDate localDate = QDate::currentDate(Qt::LocalTime); QDate utcDate = QDate::currentDate(Qt::UTC); QDate localDate = QDate::currentDate(); QDate utcDate = QDateTime::currentDateTime().toUTC().date();

See also
QDateTime::toUTC()

Definition at line 3115 of file qdatetime.cpp.

Referenced by _q_parseUnixDir(), QCalendarTextNavigator::applyDate(), QDateTime::currentMSecsSinceEpoch(), macTimeToString(), QCalendarDateValidator::QCalendarDateValidator(), QCalendarModel::QCalendarModel(), qEncodeNtlmv2Response(), qt_eval_days_left(), and QCalendarWidget::showToday().

3116 {
3117  QDate d;
3118  SYSTEMTIME st;
3119  memset(&st, 0, sizeof(SYSTEMTIME));
3120  GetLocalTime(&st);
3121  d.jd = julianDayFromDate(st.wYear, st.wMonth, st.wDay);
3122  return d;
3123 }
double d
Definition: qnumeric_p.h:62
The QDate class provides date functions.
Definition: qdatetime.h:55
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113
#define st(var, type, card)
uint jd
Definition: qdatetime.h:137

◆ day()

int QDate::day ( ) const

Returns the day of the month (1 to 31) of this date.

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

Definition at line 395 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), _q_parseDosDir(), _q_parseUnixDir(), adjustDate(), QCalendarMonthValidator::applyToDate(), QCalendarYearValidator::applyToDate(), QCalendarModel::cellForDate(), QCalendarModel::columnForFirstOfMonth(), QCalendarModel::data(), QCalendarModel::dateForCell(), QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::DayFromAbstractDateTimeFN::extract(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QPatternist::GMonthDay::fromDateTime(), QPatternist::GDay::fromDateTime(), fromString(), QDateTimeParser::getDigit(), localToUtc(), macDateToString(), macTimeToString(), operator<<(), QDateTimeToDATE(), qMakeOraDate(), QCalendarWidget::setCurrentPage(), QCalendarDayValidator::setDate(), QDateTimeParser::setDigit(), QDateTimeEditPrivate::stepBy(), QCalendarDayValidator::text(), toString(), QSystemLocalePrivate::toString(), QDateTime::toString(), QMYSQLResult::virtual_hook(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().

396 {
397  int d;
398  getDateFromJulianDay(jd, 0, 0, &d);
399  return d;
400 }
double d
Definition: qnumeric_p.h:62
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
uint jd
Definition: qdatetime.h:137

◆ dayOfWeek()

int QDate::dayOfWeek ( ) const

Returns the weekday (1 = Monday to 7 = Sunday) for this date.

See also
day(), dayOfYear(), Qt::DayOfWeek

Definition at line 408 of file qdatetime.cpp.

Referenced by QCalendarModel::columnForFirstOfMonth(), QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QDateTimeParser::parse(), QCalendarDayValidator::text(), toString(), QDateTime::toString(), and weekNumber().

409 {
410  return (jd % 7) + 1;
411 }
uint jd
Definition: qdatetime.h:137

◆ dayOfYear()

int QDate::dayOfYear ( ) const

Returns the day of the year (1 to 365 or 366 on leap years) for this date.

See also
day(), dayOfWeek()

Definition at line 420 of file qdatetime.cpp.

Referenced by QScript::DateTimeToMs(), and weekNumber().

421 {
422  return jd - julianDayFromDate(year(), 1, 1) + 1;
423 }
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
uint jd
Definition: qdatetime.h:137

◆ daysInMonth()

int QDate::daysInMonth ( ) const

Returns the number of days in the month (28 to 31) for this date.

See also
day(), daysInYear()

Definition at line 431 of file qdatetime.cpp.

Referenced by QDateTimeParser::absoluteMax(), QCalendarDayValidator::applyToDate(), fixedDate(), QCalendarView::moveCursor(), QDateTimeParser::parse(), QCalendarWidget::setCurrentPage(), QDateTimeParser::setDigit(), and QDateTimeEditPrivate::stepBy().

432 {
433  int y, m, d;
434  getDateFromJulianDay(jd, &y, &m, &d);
435  if (m == 2 && isLeapYear(y))
436  return 29;
437  else
438  return monthDays[m];
439 }
double d
Definition: qnumeric_p.h:62
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
static const char monthDays[]
Definition: qdatetime.cpp:173
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
uint jd
Definition: qdatetime.h:137

◆ daysInYear()

int QDate::daysInYear ( ) const

Returns the number of days in the year (365 or 366) for this date.

See also
day(), daysInMonth()

Definition at line 447 of file qdatetime.cpp.

448 {
449  int y, m, d;
450  getDateFromJulianDay(jd, &y, &m, &d);
451  return isLeapYear(y) ? 366 : 365;
452 }
double d
Definition: qnumeric_p.h:62
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
uint jd
Definition: qdatetime.h:137

◆ daysTo()

int QDate::daysTo ( const QDate d) const

Returns the number of days from this date to d (which is negative if d is earlier than this date).

Example:

QDate d1(1995, 5, 17); // May 17, 1995
QDate d2(1995, 5, 20); // May 20, 1995
d1.daysTo(d2); // returns 3
d2.daysTo(d1); // returns -3
See also
addDays()

Definition at line 1111 of file qdatetime.cpp.

Referenced by QCalendarModel::cellForDate(), QDateTime::daysTo(), localToUtc(), QDateTime::msecsTo(), qt_eval_days_left(), QDateTime::secsTo(), toDate(), toTimeStamp(), QCalendarWidgetPrivate::updateCurrentPage(), and utcToLocal().

1112 {
1113  return d.jd - jd;
1114 }
uint jd
Definition: qdatetime.h:137

◆ fromJulianDay()

static QDate QDate::fromJulianDay ( int  jd)
inlinestatic

Converts the Julian day jd to a QDate.

See also
toJulianDay()

Definition at line 133 of file qdatetime.h.

Referenced by QCalendarModel::QCalendarModel().

133 { QDate d; d.jd = jd; return d; }
double d
Definition: qnumeric_p.h:62
The QDate class provides date functions.
Definition: qdatetime.h:55
uint jd
Definition: qdatetime.h:137

◆ fromString() [1/2]

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

Returns the QDate represented by the string, using the format given, or an invalid date if the string cannot be parsed.

Note for Qt::TextDate: It is recommended that 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.

Definition at line 1203 of file qdatetime.cpp.

Referenced by QLibraryInfo::buildDate(), convert(), QPSQLResult::data(), QDeclarativeStringConverters::dateFromString(), QTime::fromString(), QDateTime::fromString(), QTDSResult::gotoNext(), and qDateFromString().

1204 {
1205  if (s.isEmpty())
1206  return QDate();
1207 
1208  switch (f) {
1209  case Qt::ISODate:
1210  {
1211  int year(s.mid(0, 4).toInt());
1212  int month(s.mid(5, 2).toInt());
1213  int day(s.mid(8, 2).toInt());
1214  if (year && month && day)
1215  return QDate(year, month, day);
1216  }
1217  break;
1218  case Qt::SystemLocaleDate:
1223  case Qt::LocaleDate:
1226  return fromString(s, QLocale().dateFormat(f == Qt::DefaultLocaleLongDate ? QLocale::LongFormat
1228  default:
1229 #ifndef QT_NO_TEXTDATE
1230  case Qt::TextDate: {
1231  QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
1232 
1233  if (parts.count() != 4) {
1234  return QDate();
1235  }
1236 
1237  QString monthName = parts.at(1);
1238  int month = -1;
1239  // Assume that English monthnames are the default
1240  for (int i = 0; i < 12; ++i) {
1241  if (monthName == QLatin1String(qt_shortMonthNames[i])) {
1242  month = i + 1;
1243  break;
1244  }
1245  }
1246  // If English names can't be found, search the localized ones
1247  if (month == -1) {
1248  for (int i = 1; i <= 12; ++i) {
1249  if (monthName == QDate::shortMonthName(i)) {
1250  month = i;
1251  break;
1252  }
1253  }
1254  }
1255  if (month < 1 || month > 12) {
1256  return QDate();
1257  }
1258 
1259  bool ok;
1260  int day = parts.at(2).toInt(&ok);
1261  if (!ok) {
1262  return QDate();
1263  }
1264 
1265  int year = parts.at(3).toInt(&ok);
1266  if (!ok) {
1267  return QDate();
1268  }
1269 
1270  return QDate(year, month, day);
1271  }
1272 #else
1273  break;
1274 #endif
1275  }
1276  return QDate();
1277 }
int toInt(bool *ok=0, int base=10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 ...
Definition: qstring.cpp:6090
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QDate()
Constructs a null date.
Definition: qdatetime.h:63
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
static const char *const qt_shortMonthNames[]
Definition: qdatetime.cpp:176
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ fromString() [2/2]

QDate QDate::fromString ( const QString string,
const QString format 
)
static

Returns the QDate represented by the string, using the format given, or an invalid date if the string cannot 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. For example:

QDate date = QDate::fromString("1MM12car2003", "d'MM'MMcaryyyy");
// date is 1 December 2003

If the format is not satisfied, an invalid QDate is returned. The expressions that don't 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:

QDate date = QDate::fromString("130", "Md"); // invalid

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:

QDate::fromString("1.30", "M.d"); // January 30 1900
QDate::fromString("20000110", "yyyyMMdd"); // January 10, 2000
QDate::fromString("20000110", "yyyyMd"); // January 10, 2000
See also
QDateTime::fromString(), QTime::fromString(), QDate::toString(), QDateTime::toString(), QTime::toString()

Definition at line 1347 of file qdatetime.cpp.

1348 {
1349  QDate date;
1350 #ifndef QT_BOOTSTRAPPED
1352  if (dt.parseFormat(format))
1353  dt.fromString(string, &date, 0);
1354 #else
1355  Q_UNUSED(string);
1356  Q_UNUSED(format);
1357 #endif
1358  return date;
1359 }
The QDate class provides date functions.
Definition: qdatetime.h:55
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ getDate()

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

Extracts the date's year, month, and day, and assigns them to year, *month, and *day. The pointers may be null.

See also
year(), month(), day(), isValid()

Definition at line 977 of file qdatetime.cpp.

978 {
980 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
uint jd
Definition: qdatetime.h:137

◆ gregorianToJulian()

uint QDate::gregorianToJulian ( int  y,
int  m,
int  d 
)
static

This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian().

Warning
This function is not part of the public interface.

Qt 5: remove it

Definition at line 1426 of file qdatetime.cpp.

1427 {
1428  return julianDayFromDate(y, m, d);
1429 }
double d
Definition: qnumeric_p.h:62
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113

◆ isLeapYear()

bool QDate::isLeapYear ( int  year)
static

Returns true if the specified year is a leap year; otherwise returns false.

Definition at line 1404 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), _q_parseUnixDir(), daysInMonth(), daysInYear(), isValid(), and weekNumber().

1405 {
1406  if (y < 1582) {
1407  if ( y < 1) { // No year 0 in Julian calendar, so -1, -5, -9 etc are leap years
1408  ++y;
1409  }
1410  return y % 4 == 0;
1411  } else {
1412  return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
1413  }
1414 }

◆ isNull()

bool QDate::isNull ( ) const
inline

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

Referenced by QDateTime::isNull(), isValid(), QDateTimeToDATE(), and QCalendarWidget::setDateTextFormat().

66 { return jd == 0; }
uint jd
Definition: qdatetime.h:137

◆ isValid() [1/2]

bool QDate::isValid ( ) const

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

See also
isNull()

Definition at line 340 of file qdatetime.cpp.

Referenced by addMonths(), addYears(), QCalendarModel::cellForDate(), convert(), QPatternist::AbstractDateTime::create(), QCalendarModel::data(), QCalendarModel::dateForCell(), QDeclarativeStringConverters::dateFromString(), QLocalePrivate::dateTimeToString(), QDateTimeEditPrivate::emitSignals(), QCalendarModel::flags(), fmtDateTime(), QSqlDriver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QDateTimeParser::fromString(), QCalendarView::handleMouseEvent(), QPatternist::AbstractDateTime::isRangeValid(), QDateTime::isValid(), localToUtc(), QCalendarView::mouseMoveEvent(), QCalendarView::mousePressEvent(), QCalendarView::mouseReleaseEvent(), operator<<(), QCalendarDelegate::paint(), QDateTimeParser::parse(), parseDateString(), QDateTime::QDateTime(), QDateTimeEdit::QDateTimeEdit(), QDateTimeToDATE(), QCalendarModel::referenceDate(), setDate(), QDateTimeEdit::setDate(), QDateTime::setDate(), QDateTimeEdit::setDateRange(), QCalendarWidget::setDateRange(), QDateTimeParser::setDigit(), QTime::setHMS(), QCalendarWidget::setMaximumDate(), QDateTimeEdit::setMaximumDate(), QCalendarModel::setMaximumDate(), QCalendarWidget::setMinimumDate(), QDateTimeEdit::setMinimumDate(), QCalendarModel::setMinimumDate(), QCalendarWidget::setSelectedDate(), toString(), QTest::toString(), QTime::toString(), QLocale::toString(), QCalendarWidget::updateCell(), QCalendarWidgetPrivate::updateCurrentPage(), and weekNumber().

341 {
342  return !isNull();
343 }
bool isNull() const
Returns true if the date is null; otherwise returns false.
Definition: qdatetime.h:66

◆ isValid() [2/2]

bool QDate::isValid ( int  year,
int  month,
int  day 
)
static

Returns true if the specified date (year, month, and day) is valid; otherwise returns false.

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

Example:

QDate::isValid(2002, 5, 17); // true
QDate::isValid(2002, 2, 30); // false (Feb 30 does not exist)
QDate::isValid(2004, 2, 29); // true (2004 is a leap year)
QDate::isValid(2000, 2, 29); // true (2000 is a leap year)
QDate::isValid(2006, 2, 29); // false (2006 is not a leap year)
QDate::isValid(2100, 2, 29); // false (2100 is not a leap year)
QDate::isValid(1202, 6, 6); // true (even though 1202 is pre-Gregorian)
See also
isNull(), setDate()

Definition at line 1377 of file qdatetime.cpp.

1378 {
1379  if (year < FIRST_YEAR
1380  || (year == FIRST_YEAR &&
1381  (month < FIRST_MONTH
1382  || (month == FIRST_MONTH && day < FIRST_DAY)))
1383  || year == 0) // there is no year 0 in the Julian calendar
1384  return false;
1385 
1386  // passage from Julian to Gregorian calendar
1387  if (year == 1582 && month == 10 && day > 4 && day < 15)
1388  return 0;
1389 
1390  return (day > 0 && month > 0 && month <= 12) &&
1391  (day <= monthDays[month] || (day == 29 && month == 2 && isLeapYear(year)));
1392 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
static const char monthDays[]
Definition: qdatetime.cpp:173
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353

◆ julianToGregorian()

void QDate::julianToGregorian ( uint  jd,
int &  y,
int &  m,
int &  d 
)
static

This function has a confusing name and shouldn't be part of the API anyway, since we have toJulian() and fromJulian().

Warning
This function is not part of the public interface.

Qt 5: remove it

Definition at line 1441 of file qdatetime.cpp.

1442 {
1443  getDateFromJulianDay(jd, &y, &m, &d);
1444 }
double d
Definition: qnumeric_p.h:62
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
uint jd
Definition: qdatetime.h:137

◆ longDayName() [1/2]

QString QDate::longDayName ( int  weekday)
static

Returns the long version of the name of the weekday.

The returned name is in normal type which can be used for date formatting.

See also
toString(), shortDayName(), shortMonthName(), longMonthName()

Definition at line 767 of file qdatetime.cpp.

768 {
769  if (weekday < 1 || weekday > 7) {
770  weekday = 1;
771  }
772  return QLocale::system().dayName(weekday, QLocale::LongFormat);
773 }
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString dayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on)...
Definition: qlocale.cpp:2106

◆ longDayName() [2/2]

QString QDate::longDayName ( int  weekday,
MonthNameType  type 
)
static

Returns the long name of the weekday for the representation specified by type.

Since
4.5

The days are enumerated using the following convention:

  • 1 = "Monday"
  • 2 = "Tuesday"
  • 3 = "Wednesday"
  • 4 = "Thursday"
  • 5 = "Friday"
  • 6 = "Saturday"
  • 7 = "Sunday"

The day names will be localized according to the system's default locale settings.

See also
toString(), shortDayName(), shortMonthName(), longMonthName()

Definition at line 744 of file qdatetime.cpp.

745 {
746  if (weekday < 1 || weekday > 7) {
747  weekday = 1;
748  }
749  switch (type) {
750  case QDate::DateFormat:
751  return QLocale::system().dayName(weekday, QLocale::LongFormat);
754  default:
755  break;
756  }
757  return QLocale::system().dayName(weekday, QLocale::LongFormat);
758 }
int type
Definition: qmetatype.cpp:239
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString dayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on)...
Definition: qlocale.cpp:2106
QString standaloneDayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on) tha...
Definition: qlocale.cpp:2158

◆ longMonthName() [1/2]

QString QDate::longMonthName ( int  month)
static

Returns the long version of the name of the month.

The returned name is in normal type which can be used for date formatting.

See also
toString(), shortMonthName(), shortDayName(), longDayName()

Definition at line 651 of file qdatetime.cpp.

652 {
653  if (month < 1 || month > 12) {
654  month = 1;
655  }
657 }
QString monthName(int, FormatType format=LongFormat) const
Returns the localized name of month, in the format specified by type.
Definition: qlocale.cpp:2007
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917

◆ longMonthName() [2/2]

QString QDate::longMonthName ( int  month,
MonthNameType  type 
)
static

Returns the long name of the month for the representation specified by type.

Since
4.5

The months are enumerated using the following convention:

  • 1 = "January"
  • 2 = "February"
  • 3 = "March"
  • 4 = "April"
  • 5 = "May"
  • 6 = "June"
  • 7 = "July"
  • 8 = "August"
  • 9 = "September"
  • 10 = "October"
  • 11 = "November"
  • 12 = "December"

The month names will be localized according to the system's default locale settings.

See also
toString(), shortMonthName(), shortDayName(), longDayName()

Definition at line 628 of file qdatetime.cpp.

629 {
630  if (month < 1 || month > 12) {
631  month = 1;
632  }
633  switch (type) {
634  case QDate::DateFormat:
638  default:
639  break;
640  }
641  return QString();
642 }
int type
Definition: qmetatype.cpp:239
QString monthName(int, FormatType format=LongFormat) const
Returns the localized name of month, in the format specified by type.
Definition: qlocale.cpp:2007
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString standaloneMonthName(int, FormatType format=LongFormat) const
Returns the localized name of month that is used as a standalone text, in the format specified by typ...
Definition: qlocale.cpp:2056

◆ month()

int QDate::month ( ) const

Returns the number corresponding to the month of this date, using the following convention:

  • 1 = "January"
  • 2 = "February"
  • 3 = "March"
  • 4 = "April"
  • 5 = "May"
  • 6 = "June"
  • 7 = "July"
  • 8 = "August"
  • 9 = "September"
  • 10 = "October"
  • 11 = "November"
  • 12 = "December"
See also
year(), day()

Definition at line 382 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), QCalendarWidgetPrivate::_q_monthChanged(), _q_parseDosDir(), _q_parseUnixDir(), QCalendarWidgetPrivate::_q_slotChangeDate(), adjustDate(), QCalendarDayValidator::applyToDate(), QCalendarYearValidator::applyToDate(), QCalendarWidgetPrivate::createNavigationBar(), QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::MonthFromAbstractDateTimeFN::extract(), QCalendarModel::formatForCell(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QPatternist::GMonthDay::fromDateTime(), QPatternist::GYearMonth::fromDateTime(), QPatternist::GMonth::fromDateTime(), fromString(), QDateTimeParser::getDigit(), isValid(), localToUtc(), macDateToString(), macTimeToString(), QCalendarView::moveCursor(), operator<<(), QDateTimeParser::parse(), QDateTimeParser::parseSection(), QCalendarModel::QCalendarModel(), QDateTimeToDATE(), qMakeOraDate(), QCalendarMonthValidator::setDate(), QCalendarWidget::setDateRange(), QDateTimeParser::setDigit(), QCalendarWidget::setMaximumDate(), QCalendarWidget::setMinimumDate(), QCalendarWidget::setSelectedDate(), QCalendarWidget::showSelectedDate(), QCalendarWidget::showToday(), QCalendarMonthValidator::text(), toString(), QSystemLocalePrivate::toString(), QDateTime::toString(), QCalendarWidgetPrivate::updateCurrentPage(), QCalendarWidgetPrivate::updateMonthMenu(), QMYSQLResult::virtual_hook(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().

383 {
384  int m;
385  getDateFromJulianDay(jd, 0, &m, 0);
386  return m;
387 }
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
uint jd
Definition: qdatetime.h:137

◆ operator!=()

bool QDate::operator!= ( const QDate other) const
inline

Returns true if this date is different from d; otherwise returns false.

Definition at line 108 of file qdatetime.h.

108 { return jd != other.jd; }
uint jd
Definition: qdatetime.h:137

◆ operator<()

bool QDate::operator< ( const QDate other) const
inline

Returns true if this date is earlier than d; otherwise returns false.

Definition at line 109 of file qdatetime.h.

109 { return jd < other.jd; }
uint jd
Definition: qdatetime.h:137

◆ operator<=()

bool QDate::operator<= ( const QDate other) const
inline

Returns true if this date is earlier than or equal to d; otherwise returns false.

Definition at line 110 of file qdatetime.h.

110 { return jd <= other.jd; }
uint jd
Definition: qdatetime.h:137

◆ operator==()

bool QDate::operator== ( const QDate other) const
inline

Returns true if this date is equal to d; otherwise returns false.

Definition at line 107 of file qdatetime.h.

107 { return jd == other.jd; }
uint jd
Definition: qdatetime.h:137

◆ operator>()

bool QDate::operator> ( const QDate other) const
inline

Returns true if this date is later than d; otherwise returns false.

Definition at line 111 of file qdatetime.h.

111 { return jd > other.jd; }
uint jd
Definition: qdatetime.h:137

◆ operator>=()

bool QDate::operator>= ( const QDate other) const
inline

Returns true if this date is later than or equal to d; otherwise returns false.

Definition at line 112 of file qdatetime.h.

112 { return jd >= other.jd; }
uint jd
Definition: qdatetime.h:137

◆ setDate()

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

Sets the date's year, month, and day.

Since
4.2

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.

See also
isValid()

Definition at line 959 of file qdatetime.cpp.

Referenced by adjustDate(), fixedDate(), QDate(), and setYMD().

960 {
961  if (!isValid(year, month, day)) {
962  jd = 0;
963  } else {
965  }
966  return jd != 0;
967 }
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
static uint julianDayFromDate(int year, int month, int day)
Definition: qdatetime.cpp:113
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
uint jd
Definition: qdatetime.h:137

◆ setYMD()

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

Sets the date's year y, month m, and day d.

If y is in the range 0 to 99, it is interpreted as 1900 to 1999.

Use setDate() instead.

Definition at line 937 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), and operator>>().

938 {
939  if (uint(y) <= 99)
940  y += 1900;
941  return setDate(y, m, d);
942 }
double d
Definition: qnumeric_p.h:62
unsigned int uint
Definition: qglobal.h:996
bool setDate(int year, int month, int day)
Sets the date&#39;s year, month, and day.
Definition: qdatetime.cpp:959

◆ shortDayName() [1/2]

QString QDate::shortDayName ( int  weekday)
static

Returns the short version of the name of the weekday.

The returned name is in normal type which can be used for date formatting.

See also
toString(), longDayName(), shortMonthName(), longMonthName()

Definition at line 709 of file qdatetime.cpp.

Referenced by toString(), and QDateTime::toString().

710 {
711  if (weekday < 1 || weekday > 7) {
712  weekday = 1;
713  }
714  return QLocale::system().dayName(weekday, QLocale::ShortFormat);
715 }
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString dayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on)...
Definition: qlocale.cpp:2106

◆ shortDayName() [2/2]

QString QDate::shortDayName ( int  weekday,
MonthNameType  type 
)
static

Returns the short name of the weekday for the representation specified by type.

Since
4.5

The days are enumerated using the following convention:

  • 1 = "Mon"
  • 2 = "Tue"
  • 3 = "Wed"
  • 4 = "Thu"
  • 5 = "Fri"
  • 6 = "Sat"
  • 7 = "Sun"

The day names will be localized according to the system's default locale settings.

See also
toString(), shortMonthName(), longMonthName(), longDayName()

Definition at line 686 of file qdatetime.cpp.

687 {
688  if (weekday < 1 || weekday > 7) {
689  weekday = 1;
690  }
691  switch (type) {
692  case QDate::DateFormat:
693  return QLocale::system().dayName(weekday, QLocale::ShortFormat);
696  default:
697  break;
698  }
699  return QString();
700 }
int type
Definition: qmetatype.cpp:239
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString dayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on)...
Definition: qlocale.cpp:2106
QString standaloneDayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on) tha...
Definition: qlocale.cpp:2158

◆ shortMonthName() [1/2]

QString QDate::shortMonthName ( int  month)
static

Returns the short version of the name of the month.

The returned name is in normal type which can be used for date formatting.

See also
toString(), longMonthName(), shortDayName(), longDayName()

Definition at line 591 of file qdatetime.cpp.

Referenced by fromShortMonthName(), fromString(), toString(), and QDateTime::toString().

592 {
594 }
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591

◆ shortMonthName() [2/2]

QString QDate::shortMonthName ( int  month,
QDate::MonthNameType  type 
)
static

Returns the short name of the month for the representation specified by type.

Since
4.5

The months are enumerated using the following convention:

  • 1 = "Jan"
  • 2 = "Feb"
  • 3 = "Mar"
  • 4 = "Apr"
  • 5 = "May"
  • 6 = "Jun"
  • 7 = "Jul"
  • 8 = "Aug"
  • 9 = "Sep"
  • 10 = "Oct"
  • 11 = "Nov"
  • 12 = "Dec"

The month names will be localized according to the system's default locale settings.

See also
toString(), longMonthName(), shortDayName(), longDayName()

Definition at line 568 of file qdatetime.cpp.

569 {
570  if (month < 1 || month > 12) {
571  month = 1;
572  }
573  switch (type) {
574  case QDate::DateFormat:
578  default:
579  break;
580  }
581  return QString();
582 }
int type
Definition: qmetatype.cpp:239
QString monthName(int, FormatType format=LongFormat) const
Returns the localized name of month, in the format specified by type.
Definition: qlocale.cpp:2007
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString standaloneMonthName(int, FormatType format=LongFormat) const
Returns the localized name of month that is used as a standalone text, in the format specified by typ...
Definition: qlocale.cpp:2056

◆ toJulianDay()

int QDate::toJulianDay ( ) const
inline

Converts the date to a Julian day.

See also
fromJulianDay()

Definition at line 134 of file qdatetime.h.

Referenced by QDeclarativeCompiler::genLiteralAssignment(), localToUtc(), QDateTimeParser::parse(), and utcToLocal().

134 { return jd; }
uint jd
Definition: qdatetime.h:137

◆ toString() [1/2]

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

Returns the date as a string.

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

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 the format 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.

Warning
The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to locale-aware formats as well, depending on the locale settings.
See also
shortDayName(), shortMonthName()

Definition at line 823 of file qdatetime.cpp.

Referenced by QDeclarativeEnginePrivate::formatDate(), QSqlDriver::formatValue(), operator<<(), QTest::toString(), and QDateTime::toString().

824 {
825  if (!isValid())
826  return QString();
827  int y, m, d;
828  getDateFromJulianDay(jd, &y, &m, &d);
829  switch (f) {
835  case Qt::LocaleDate:
839  : QLocale::ShortFormat);
840  default:
841 #ifndef QT_NO_TEXTDATE
842  case Qt::TextDate:
843  {
844  return QString::fromLatin1("%0 %1 %2 %3")
846  .arg(shortMonthName(m))
847  .arg(d)
848  .arg(y);
849  }
850 #endif
851  case Qt::ISODate:
852  {
853  if (year() < 0 || year() > 9999)
854  return QString();
855  QString month(QString::number(m).rightJustified(2, QLatin1Char('0')));
856  QString day(QString::number(d).rightJustified(2, QLatin1Char('0')));
857  return QString::number(y) + QLatin1Char('-') + month + QLatin1Char('-') + day;
858  }
859  }
860 }
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
int dayOfWeek() const
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
Definition: qdatetime.cpp:408
double d
Definition: qnumeric_p.h:62
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
static QString shortDayName(int weekday)
Returns the short version of the name of the weekday.
Definition: qdatetime.cpp:709
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition: qlocale.cpp:1295
static QLocale system()
Returns a QLocale object initialized to the system locale.
Definition: qlocale.cpp:1917
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
static QString shortMonthName(int month)
Returns the short version of the name of the month.
Definition: qdatetime.cpp:591
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
uint jd
Definition: qdatetime.h:137
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ toString() [2/2]

QString QDate::toString ( const QString format) const

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.

Warning
The Qt::ISODate format is only valid for years in the range 0 to 9999. This restriction may apply to locale-aware formats as well, depending on the locale settings.
See also
QDateTime::toString() QTime::toString()

Definition at line 915 of file qdatetime.cpp.

916 {
917  if (year() > 9999)
918  return QString();
919  return fmtDateTime(format, 0, this);
920 }
static QString fmtDateTime(const QString &f, const QTime *dt=0, const QDate *dd=0)
Definition: qdatetime.cpp:4133
The QString class provides a Unicode character string.
Definition: qstring.h:83
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353

◆ weekNumber()

int QDate::weekNumber ( int *  yearNumber = 0) 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.

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

Copyright (c) 1989 The Regents of the University of California. All rights reserved.

Redistribution and use in source and binary forms are permitted provided that the above copyright notice and this paragraph are duplicated in all such forms and that any documentation, advertising materials, and other materials related to such distribution and use acknowledge that the software was developed by the University of California, Berkeley. The name of the University may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

See also
isValid()

Definition at line 487 of file qdatetime.cpp.

Referenced by QCalendarModel::data().

488 {
489  if (!isValid())
490  return 0;
491 
492  int year = QDate::year();
493  int yday = dayOfYear() - 1;
494  int wday = dayOfWeek();
495  if (wday == 7)
496  wday = 0;
497  int w;
498 
499  for (;;) {
500  int len;
501  int bot;
502  int top;
503 
504  len = isLeapYear(year) ? 366 : 365;
505  /*
506  ** What yday (-3 ... 3) does
507  ** the ISO year begin on?
508  */
509  bot = ((yday + 11 - wday) % 7) - 3;
510  /*
511  ** What yday does the NEXT
512  ** ISO year begin on?
513  */
514  top = bot - (len % 7);
515  if (top < -3)
516  top += 7;
517  top += len;
518  if (yday >= top) {
519  ++year;
520  w = 1;
521  break;
522  }
523  if (yday >= bot) {
524  w = 1 + ((yday - bot) / 7);
525  break;
526  }
527  --year;
528  yday += isLeapYear(year) ? 366 : 365;
529  }
530  if (yearNumber != 0)
531  *yearNumber = year;
532  return w;
533 }
int dayOfWeek() const
Returns the weekday (1 = Monday to 7 = Sunday) for this date.
Definition: qdatetime.cpp:408
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
int dayOfYear() const
Returns the day of the year (1 to 365 or 366 on leap years) for this date.
Definition: qdatetime.cpp:420
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353

◆ year()

int QDate::year ( ) const

Returns the year of this date.

Negative numbers indicate years before 1 A.D. = 1 C.E., such that year -44 is 44 B.C.

See also
month(), day()

Definition at line 353 of file qdatetime.cpp.

Referenced by _q_fixupDateTime(), _q_parseDosDir(), _q_parseUnixDir(), QCalendarWidgetPrivate::_q_slotChangeDate(), QCalendarWidgetPrivate::_q_yearEditingFinished(), QCalendarDayValidator::applyToDate(), QCalendarMonthValidator::applyToDate(), QCalendarWidgetPrivate::createNavigationBar(), QScript::DateTimeToMs(), QLocalePrivate::dateTimeToString(), dayOfYear(), QDB2Result::exec(), QODBCResult::exec(), QPatternist::YearFromAbstractDateTimeFN::extract(), QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QPatternist::GYearMonth::fromDateTime(), QPatternist::GYear::fromDateTime(), fromString(), QDateTimeParser::getDigit(), localToUtc(), macDateToString(), macTimeToString(), QCalendarView::moveCursor(), operator<<(), QDateTimeParser::parseSection(), QDateTimeParser::potentialValue(), QCalendarModel::QCalendarModel(), QDateTimeToDATE(), qMakeOraDate(), QCalendarYearValidator::setDate(), QCalendarWidget::setDateRange(), QDateTimeParser::setDigit(), QCalendarWidget::setMaximumDate(), QCalendarWidget::setMinimumDate(), QCalendarWidget::setSelectedDate(), QCalendarWidget::showSelectedDate(), QCalendarWidget::showToday(), QCalendarYearValidator::text(), toString(), QSystemLocalePrivate::toString(), QDateTime::toString(), QCalendarWidgetPrivate::updateCurrentPage(), QCalendarWidgetPrivate::updateMonthMenu(), QMYSQLResult::virtual_hook(), weekNumber(), QPdfEnginePrivate::writeInfo(), and writeMSDosDate().

354 {
355  int y;
356  getDateFromJulianDay(jd, &y, 0, 0);
357  return y;
358 }
static void getDateFromJulianDay(uint julianDay, int *year, int *month, int *day)
Definition: qdatetime.cpp:133
uint jd
Definition: qdatetime.h:137

Friends and Related Functions

◆ operator<<

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

Writes the date to stream out.

See also
{Serializing Qt Data Types}

Definition at line 3812 of file qdatetime.cpp.

3813 {
3814  return out << (quint32)(date.jd);
3815 }
unsigned int quint32
Definition: qglobal.h:938
uint jd
Definition: qdatetime.h:137

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

3829 {
3830  quint32 jd;
3831  in >> jd;
3832  date.jd = jd;
3833  return in;
3834 }
unsigned int quint32
Definition: qglobal.h:938
uint jd
Definition: qdatetime.h:137

◆ QDateTime

friend class QDateTime
friend

Definition at line 139 of file qdatetime.h.

◆ QDateTimePrivate

friend class QDateTimePrivate
friend

Definition at line 140 of file qdatetime.h.

Properties

◆ jd

uint QDate::jd
private

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