| Classes - Annotated - Tree - Functions - Home - Structure

# QDate Class Reference

The QDate class provides date functions. More...

`#include <qdatetime.h>`

List of all member functions.

## Public Members

- QDate ()
- QDate ( int y, int m, int d )
- bool isNull () const
- bool isValid () const
- int year () const
- int month () const
- int day () const
- int dayOfWeek () const
- int dayOfYear () const
- int daysInMonth () const
- int daysInYear () const
- virtual QString monthName ( int month ) const
- virtual QString dayName ( int weekday ) const
- QString toString () const
- bool setYMD ( int y, int m, int d )
- QDate addDays ( int days ) const
- int daysTo ( const QDate & ) const
- bool operator== ( const QDate & d ) const
- bool operator!= ( const QDate & d ) const
- bool operator< ( const QDate & d ) const
- bool operator<= ( const QDate & d ) const
- bool operator> ( const QDate & d ) const
- bool operator>= ( const QDate & d ) const

## Static Public Members

- QDate currentDate ()
- bool isValid ( int y, int m, int d )
- bool leapYear ( int year )

## Static Protected Members

- uint greg2jul ( int y, int m, int d ) (internal)
- void jul2greg ( uint jd, int & y, int & m, int & d ) (internal)

## Related Functions

(Note that these are not member functions.)

- QDataStream & operator<< (QDataStream & s, const QDate & d)
- QDataStream & operator>> (QDataStream & s, QDate & d)

## Detailed Description

The QDate class provides date functions.

A QDate object contains a calendar date, i.e. year, month, and day numbers in the modern western (Gregorian) calendar. It can read the current date from the system clock. It provides functions for comparing dates and for manipulating a date by adding a number of days.

A QDate object is typically created either by giving the year, month and day numbers explicitly, or by using the static function currentDate(), which makes a QDate object which contains the system's clock date. An explicit date can also be set using setYMD().

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(), dayName(), and monthName() functions.

QDate provides a full set of operators to compare two QDate objects. A date is considered smaller than another if it is earlier than the other.

The date a given number of days later than a given date can be found using the addDays() function. Correspondingly, the number of days between two dates can be found using the daysTo() function.

The daysInMonth() and daysInYear() functions tell how many days there are in this date's month and year, respectively. The isLeapYear() function tells whether this date is in a leap year.

Note that QDate may not be used for date calculations for dates in the remote past, i.e. prior to the introduction of the Gregorian calendar. This calendar was adopted by England Sep. 14. 1752 (hence this is the earliest valid QDate), and subsequently by most other western countries, until 1923.

The end of time is reached around 8000AD, by which time we expect Qt to be obsolete.

See also QTime and QDateTime.

## Member Function Documentation

### QDate::QDate ()

Constructs a null date. Null dates are invalid.

See also isNull() and isValid().

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

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

y must be in the range 1752-ca. 8000, m must be in the range 1-12, and d must be in the range 1-31. Exception: if y is in the range 0-99, it is interpreted as 1900-1999.

See also isValid().

### 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 daysTo().

### QDate QDate::currentDate () `[static]`

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

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

### int QDate::day () const

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

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

### QString QDate::dayName ( int weekday ) const `[virtual]`

Returns the name of the weekday.

Weekday 1 == "Mon", day 2 == "Tue" etc.

See also toString() and monthName().

### int QDate::dayOfWeek () const

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

See also day() and dayOfYear().

### int QDate::dayOfYear () const

Returns the day of the year (1..365) for this date.

See also day() and dayOfWeek().

### int QDate::daysInMonth () const

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

See also day() and daysInYear().

### int QDate::daysInYear () const

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

See also day() and daysInMonth().

### 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 17th 1995
        QDate d2( 1995, 5, 20 );            // May 20th 1995
        d1.daysTo( d2 );                    // returns 3
        d2.daysTo( d1 );                    // returns -3

See also addDays().

### bool QDate::isNull () const

Returns TRUE if the date is null. A null date is invalid.

See also isValid().

### bool QDate::isValid () const

Returns TRUE if this date is valid.

See also isNull().

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

Returns TRUE if the specified date (year y, month m and day d) is valid.

Example:

        QDate::isValid( 2002, 5, 17 );      // TRUE;  May 17th 2002 is OK.
        QDate::isValid( 2002, 2, 30 );      // FALSE; Feb 30th does not exist
        QDate::isValid( 2004, 2, 29 );      // TRUE; 2004 is a leap year
        QDate::isValid( 1202, 6, 6 );       // FALSE; 1202 is pre-Gregorian

Note that a y value in the range 00-99 is interpreted as 1900-1999.

See also isNull() and setYMD().

### bool QDate::leapYear ( int y ) `[static]`

Returns TRUE if the specified year y is a leap year.

### int QDate::month () const

Returns the month (January=1 .. December=12) of this date.

See also year() and day().

### QString QDate::monthName ( int month ) const `[virtual]`

Returns the name of the month.

Month 1 == "Jan", month 2 == "Feb" etc.

See also toString() and dayName().

### bool QDate::operator!= ( const QDate & d ) const

Returns TRUE if this date is different from d, or FALSE if they are equal.

### bool QDate::operator< ( const QDate & d ) const

Returns TRUE if this date is earlier than d, otherwise FALSE.

### bool QDate::operator<= ( const QDate & d ) const

Returns TRUE if this date is earlier than or equal to d, otherwise FALSE.

### bool QDate::operator== ( const QDate & d ) const

Returns TRUE if this date is equal to d, or FALSE if they are different.

### bool QDate::operator> ( const QDate & d ) const

Returns TRUE if this date is later than d, otherwise FALSE.

### bool QDate::operator>= ( const QDate & d ) const

Returns TRUE if this date is later than or equal to d, otherwise FALSE.

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

Sets the year y, month m and day d.

y must be in the range 1752-ca. 8000, m must be in the range 1-12, and d must be in the range 1-31. Exception: if y is in the range 0-99, it is interpreted as 1900-1999.

Returns TRUE if the date is valid, otherwise FALSE.

### QString QDate::toString () const

Returns the date as a string.

The string format is "Sat May 20 1995". This function uses the dayName() and monthName() functions to generate the string.

See also dayName() and monthName().

### int QDate::year () const

Returns the year (>= 1752) of this date.

See also month() and day().

### uint QDate::greg2jul ( int y, int m, int d ) `[static protected]`

For internal use only.

### void QDate::jul2greg ( uint jd, int & y, int & m, int & d ) `[static protected]`

For internal use only.

## Related Functions

### QDataStream & operator<< (QDataStream & s, const QDate & d)

Writes the date to the stream.

See also Format of the QDataStream operators

### QDataStream & operator>> (QDataStream & s, QDate & d)

Reads a date from the stream.

See also Format of the QDataStream operators

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.