Wt  4.11.1
Public Member Functions | Static Public Member Functions | List of all members
Wt::WDate Class Reference

A value class that represents a date on the Gregorian calendar. More...

#include <Wt/WDate.h>

Public Member Functions

 WDate ()
 Construct a Null date. More...
 
 WDate (const std::chrono::system_clock::time_point &tp)
 Construct a date with a time_point.
 
 WDate (int year, int month, int day)
 Specify a date by year, month, and day. More...
 
void setDate (int year, int month, int day)
 Sets the date by year, month, and day. More...
 
void setTimePoint (const std::chrono::system_clock::time_point &tp)
 Sets the date from a time point.
 
WDate addDays (int ndays) const
 Adds days. More...
 
WDate addMonths (int nmonths) const
 Adds months. More...
 
WDate addYears (int nyears) const
 Adds years. More...
 
bool isNull () const
 Returns if this date is Null. More...
 
bool isValid () const
 Returns if this date is valid. More...
 
int year () const
 Returns the year. More...
 
int month () const
 Returns the month (1-12). More...
 
int day () const
 Returns the day of month (1-31). More...
 
int dayOfWeek () const
 Returns the day of week (1-7). More...
 
int daysTo (const WDate &date) const
 Returns the difference between two dates (in days). More...
 
int toJulianDay () const
 Converts the date to a Julian day. More...
 
bool operator< (const WDate &other) const
 Compares two dates.
 
bool operator<= (const WDate &other) const
 Compares two dates.
 
bool operator> (const WDate &other) const
 Compares two dates.
 
bool operator>= (const WDate &other) const
 Compares two dates.
 
bool operator== (const WDate &other) const
 Compares two dates.
 
bool operator!= (const WDate &other) const
 Compares two dates.
 
WString toString () const
 Formats this date to a string using a default format. More...
 
WString toString (const WString &format, bool localizedString=true) const
 Formats this date to a string using a specified format. More...
 

Static Public Member Functions

static WDate fromString (const WString &s)
 Parses a string to a date using a default format. More...
 
static WDate fromString (const WString &s, const WString &format)
 Parses a string to a date using a specified format. More...
 
static WDate fromJulianDay (int jd)
 Converts a Julian Day jd to a WDate . More...
 
static WDate currentDate ()
 Reports the current client date. More...
 
static WDate currentServerDate ()
 Reports the current server date. More...
 
static bool isLeapYear (int year)
 Returns whether the given year is a leap year. More...
 
static WDate previousWeekday (WDate &d, int weekday)
 Returns a date object representing the previous weekday. More...
 
static Wt::WString shortDayName (int weekday, bool localizedString=true)
 Returns the short day name. More...
 
static Wt::WString shortMonthName (int month, bool localizedString=true)
 Returns the short month name. More...
 
static Wt::WString longDayName (int weekday, bool localizedString=true)
 Returns the long day name. More...
 
static Wt::WString longMonthName (int month, bool localizedString=true)
 Returns the long month name. More...
 

Detailed Description

A value class that represents a date on the Gregorian calendar.

Class which holds a date on the gregorian calendar, specified as day/month/year.

A WDate may either specify a valid date, or be a Null date (using the default constructor WDate::WDate(), for which isNull() returns true).

A valid date may be specified by year, month, and day of month (using the WDate(year, month, day) constructor, or the setDate() method). When attempting to specify an invalid date (with an impossible combination of year/month/date), isIvalid() will return false.

The class provides a flexible way for converting between strings and dates. Use toString() to convert to strings, and fromString() for parsing strings. Both methods take a format string, and the same format syntax is supported by both methods.

Simple operations are supported to compare dates, or to calculate with dates.

i18n

If the thread using a WDate is bound to a WApplication (i.e. WApplication::instance() does not return 0), the date strings can be localized by overriding the default values for the localized string keys in the resource bundles of the WApplication:

If the thread using a WDate is not bound to a WApplication (i.e. WApplication::instance() returns 0), english strings will be used.

Internationalization affects both WDate to string conversions and string to WDate conversion.

Constructor & Destructor Documentation

◆ WDate() [1/2]

Wt::WDate::WDate ( )

Construct a Null date.

A date for which isNull() returns true. A Null date is also invalid.

See also
isValid(), isNull()

◆ WDate() [2/2]

Wt::WDate::WDate ( int  year,
int  month,
int  day 
)

Specify a date by year, month, and day.

The month has range 1-12 and the day has range 1-31. When the date is invalid, isValid() is set to false.

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

Member Function Documentation

◆ addDays()

WDate Wt::WDate::addDays ( int  ndays) const

Adds days.

Returns a date that is ndays later than this date. Negative values for ndays will result in a date that is as many days earlier.

Returns a null date if the current date is invalid or the new date is out of range.

See also
addMonths(), addYears()

◆ addMonths()

WDate Wt::WDate::addMonths ( int  nmonths) const

Adds months.

Returns a date that is the same day of the month, but nmonths later than this date. Negative values for nmonths will result in a date that is as many months earlier.

If the day does not exist in the resulting month/year then the date is set to the last day of that month (e.g. 2019-05-31 + 1 month = 2019-06-30)

Returns a null date if the current date is invalid or the new date is out of range.

See also
addDays(), addYears()

◆ addYears()

WDate Wt::WDate::addYears ( int  nyears) const

Adds years.

Returns a date that is nyears later than this date. Negative values for nyears will result in a date that is as many years earlier.

If the day does not exist in the resulting month/year then the date is set to the last day of that month (e.g. 2016-02-29 + 1 year = 2017-02-28)

Returns a null date if the current date is invalid or the new date is out of range.

See also
addDays(), addMonths()

◆ currentDate()

WDate Wt::WDate::currentDate ( )
static

Reports the current client date.

This method uses browser information to retrieve the date that is configured in the client.

See also
WLocalDateTime::currentDate()

◆ currentServerDate()

WDate Wt::WDate::currentServerDate ( )
static

Reports the current server date.

This method returns the local date on the server.

See also
WDateTime::currentDateTime(), WLocalDateTime::currentServerDateTime()

◆ day()

int Wt::WDate::day ( ) const

Returns the day of month (1-31).

Returns 0 if the date is invalid.

◆ dayOfWeek()

int Wt::WDate::dayOfWeek ( ) const

Returns the day of week (1-7).

Returns the day of week, from Monday (=1) to Sunday (=7).

Returns 0 if the date is invalid.

◆ daysTo()

int Wt::WDate::daysTo ( const WDate date) const

Returns the difference between two dates (in days).

Returns 0 if either date is invalid.

◆ fromJulianDay()

WDate Wt::WDate::fromJulianDay ( int  jd)
static

Converts a Julian Day jd to a WDate .

See also
toJulianDay() const

◆ fromString() [1/2]

WDate Wt::WDate::fromString ( const WString s)
static

Parses a string to a date using a default format.

The default format is "ddd MMM d yyyy". For example, a date specified as:

"Wed Aug 29 2007"

will be parsed as a date that equals a date constructed as:

WDate d(2007,8,29);
WDate()
Construct a Null date.
Definition: WDate.C:31

When the date could not be parsed or is not valid, an invalid date is returned (for which isValid() returns false).

See also
fromString(const WString& s, const WString& format), isValid()

◆ fromString() [2/2]

WDate Wt::WDate::fromString ( const WString s,
const WString format 
)
static

Parses a string to a date using a specified format.

The format follows the same syntax as used by toString(const WString& format).

When the date could not be parsed or is not valid, an invalid date is returned (for which isValid() returns false).

See also
toString(const WString&) const

◆ isLeapYear()

bool Wt::WDate::isLeapYear ( int  year)
static

Returns whether the given year is a leap year.

A leap year has 29 days in February, in case you wondered.

◆ isNull()

bool Wt::WDate::isNull ( ) const

Returns if this date is Null.

A null date is also invalid.

See also
isValid(), WDate()

◆ isValid()

bool Wt::WDate::isValid ( ) const

Returns if this date is valid.

See also
isNull(), WDate(int, int, int), setDate()

◆ longDayName()

WString Wt::WDate::longDayName ( int  weekday,
bool  localizedString = true 
)
static

Returns the long day name.

Results (for given weekDay) are (default English):
"Monday" (1),
"Tuesday" (2),
"Wednesday" (3),
"Thursday" (4),
"Friday" (5),
"Saturday" (6),
"Sunday" (7).

The result is affected by localization using the "Wt.WDate.Monday" to "Wt.WDate.Sunday" keys.

See also
shortDayName()

◆ longMonthName()

WString Wt::WDate::longMonthName ( int  month,
bool  localizedString = true 
)
static

Returns the long month name.

Results (for given month) are (default English):
"January" (1),
"February" (2),
"March" (3),
"April" (4),
"May" (5),
"June" (6),
"July" (7),
"August" (8),
"September" (9),
"October" (10),
"November" (11),
"December" (12).

The result is affected by localization using the "Wt.WDate.January" to "Wt.WDate.December" keys.

See also
shortDayName()

◆ month()

int Wt::WDate::month ( ) const

Returns the month (1-12).

Returns 0 if the date is invalid.

◆ previousWeekday()

WDate Wt::WDate::previousWeekday ( WDate d,
int  weekday 
)
static

Returns a date object representing the previous weekday.

Weekday [1 - 7]

◆ setDate()

void Wt::WDate::setDate ( int  year,
int  month,
int  day 
)

Sets the date by year, month, and day.

The month has range 1-12 and the day has range 1-31. When the new date is invalid, isValid() will return false.

See also
WDate(int year, int month, int day), year(), month(), day()

◆ shortDayName()

WString Wt::WDate::shortDayName ( int  weekday,
bool  localizedString = true 
)
static

Returns the short day name.

Results (for given weekDay) are (default English):
"Mon" (1),
"Tue" (2),
"Wed" (3),
"Thu" (4),
"Fri" (5),
"Sat" (6),
"Sun" (7).

The result is affected by localization using the "Wt.WDate.3.Mon" to "Wt.WDate.3.Sun" keys.

See also
longDayName()

◆ shortMonthName()

WString Wt::WDate::shortMonthName ( int  month,
bool  localizedString = true 
)
static

Returns the short month name.

Results (for given month) are (default English):
"Jan" (1),
"Feb" (2),
"Mar" (3),
"Apr" (4),
"May" (5),
"Jun" (6),
"Jul" (7),
"Aug" (8),
"Sep" (9),
"Oct" (10),
"Nov" (11),
"Dec" (12)
.

The result is affected by localization using the "Wt.WDate.3.Jan" to "Wt.WDate.3.Dec" keys.

See also
longMonthName()

◆ toJulianDay()

int Wt::WDate::toJulianDay ( ) const

Converts the date to a Julian day.

Returns 0 if the date is invalid.

See also
fromJulianDay()

◆ toString() [1/2]

WString Wt::WDate::toString ( ) const

Formats this date to a string using a default format.

The default format is "ddd MMM d yyyy". For example, a date constructed as:

WDate d(2007,8,29);

will be formatted as:

"Wed Aug 29 2007"
See also
toString(const WString& format) const, fromString()

◆ toString() [2/2]

WString Wt::WDate::toString ( const WString format,
bool  localizedString = true 
) const

Formats this date to a string using a specified format.

The format is a string in which the following contents has a special meaning.

CodeMeaning Example (for Mon Aug 3 2007)
dThe day without leading zero (1-31) 3
ddThe day with leading zero (01-31) 03
dddThe day abbreviated using shortDayName() Mon
ddddThe day abbreviated using longDayName() Monday
MThe month without leading zero (1-12) 8
MMThe month with leading zero (01-12) 08
MMMThe month abbreviated using shortMonthName() Aug
MMMMThe month abbreviated using longMonthName() August
yyThe year as a two-digit number (00-99) 07
yyyyThe year as a four-digit number (-9999-9999) 2007

Any other text is kept literally. String content between single quotes (') are not interpreted as special codes. LabelOption::Inside a string, a literal quote may be specifed using a double quote ('').

Examples of format and result:

FormatResult (for Mon Aug 3 2007)
ddd MMM d yyyyMon Aug 3 2007
dd/MM/yyyy03/08/2007
dddd, MMM d, yyyyWednesday, Aug 3, 2007
'MM': MM, 'd': d, 'yyyy': yyyyMM: 08, d: 3, yyyy: 2007

The localizedString allows for the potential strings that are created to be localized or not.

E.g. If "ddd" is part of the format, the abbreviated day is used. If localizedString is true, and if the abbreviations are defined in a resource bundle (overwriting the default "Wt.WDate.3.Mon" for example), they will be used.

See also
fromString(const WString& value, const WString& format)

◆ year()

int Wt::WDate::year ( ) const

Returns the year.

Returns 0 if the date is invalid.