Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
C++ Boost



Overall Index -- Gregorian Index -- Posix Time Index

Date Documentation

Header -- Construction -- Construct from String -- Construct from Clock -- Accessors -- Conversion To String -- Operators


The class boost::gregorian::date is the primary interface for date programming. In general, the date class is immutable once constructed although it does allow assignment.

Other techniques for creating dates include date iterators and date algorithms or generators.


#include "boost/date_time/gregorian/gregorian.hpp" //include all types plus i/o
#include "boost/date_time/gregorian/gregorian_types.hpp" //no i/o just types


date(greg_year year, greg_month month, greg_day day) Construct from parts of date. Throws bad_year, bad_day_of_month, or bad_month (derivatives of std::out_of_range) if the year, month or day are out of range. date d(2002,Jan,10)
date(date d) Copy constructor date d1(d)
date(special_values sv) Constructor for infinities, not-a-date-time, max_date, and min_date date d1(neg_infin);
date d2(pos_infin);
date d3(not_a_date_time);
date d4(max_date);
date d5(min_date);

Construction From String

The various delimited string parsers will parse dates with various types of delimiters such as '-', '/', or spaces. In addition, the delimited parsers accept months as either a numeric value or as a short or long case-insensitive string. For example, the from_string function will accept all the following strings for the date Jan 25, 2002.

date from_string(const std::string&) From delimited date string where with order iso standard ordering: year-month-day (eg: 2002-1-25) std::string ds("2002/1/25"); //or 2002-Jan-25, etc
date d(from_string(ds))
date from_us_string(const std::string&) A delimited date string where with order month-day-year eg: 1-25-2002 std::string ds("1/25/2002");
date d(from_us_string(ds))
date from_uk_string(const std::string&) A delimited date string where with order day-month-year eg: 25-1-2002 std::string ds("25/1/2002");
date d(from_us_string(ds))
date from_undelimited_string(const std::string&) From iso type date string where with order year-month-day eg: 20020125 std::string ds("20020125");
date d(from_undelimited_string(ds))

Construction From Clock

day_clock::local_day() Get the local day based on the time zone settings of the computer. date d(day_clock::local_day())
day_clock::universal_day() Get the UTC (Universal Time Coordinated) day based on the time zone settings of the local computer. date d(day_clock::universal_day())


greg_year year() const Get the year part of the date. date d(2002,Jan,10); d.year() --> 2002;
greg_month month() const Get the month part of the date. date d(2002,Jan,10); d.month() --> 1;
greg_day day() const Get the day part of the date. date d(2002,Jan,10); --> 10;
greg_ymd year_month_day() const Return a year_month_day struct. More efficient when all 3 parts of the date are needed. date d(2002,Jan,10);
date::ymd_type ymd = d.year_month_day();
ymd.year --> 2002, ymd.month --> 1, --> 10
greg_day_of_week day_of_week() const Get the day of the week (eg: Sunday, Monday, etc. date d(2002,Jan,10); --> Thursday;
bool is_infinity() const Returns true if date is either positive or negative infinity date d(pos_infin); d.is_infinity() --> true;
bool is_neg_infinity() const Returns true if date is negative infinity date d(neg_infin); d.is_neg_infinity() --> true;
bool is_pos_infinity() const Returns true if date is positive infinity date d(neg_infin); d.is_pos_infinity() --> true;
bool is_not_a_date() const Returns true if value is not a date date d(not_a_date_time);
d.is_not_a_date() --> true;
long modjulian_day() const Returns the modified julian day for the date.
long julian_day() const Returns the julian day for the date.
int week_number() const Returns the ISO 8601 week number for the date.

Conversion To String

These convenience functions provide simple ways to converts a date into a string. The functions are in large part here to support legacy compilers that do not correctly support locales. See operator<< below for a normal way to convert a date into a string.

std::string to_simple_string(date d) To YYYY-mmm-DD string where mmm 3 char month name. 2002-Jan-01
std::string to_iso_string(date d) To YYYYMMDD where all components are integers. 20020131
std::string to_iso_extended_string(date d) To YYYY-MM-DD where all components are integers. 2002-01-31


operator<< Stream output operator. Format of the output is subject to the current localization settings. See I/O localization for more. date d(2002,Jan,1)
std::cout << d << std::endl;
operator==, operator!=,
operator>, operator<
operator>=, operator<=
A full complement of comparison operators d1 == d2, etc
date operator+(date_duration) const Return a date adding a day offset date d(2002,Jan,1);
date_duration dd(1);
date d2 = d + dd;
date operator-(date_duration) const Return a date by adding a day offset date d(2002,Jan,1);
date_duration dd(1);
date d2 = d - dd;
date_duration operator-(date) const Return a date duration by subtracting two dates date d1(2002,Jan,1);
date d2(2002,Jan,2);
date_duration dd = d2-d1;

Last modified: Mon Jan 19 21:34:44 MST 2004 by Jeff Garland © 2000-2002