boost datetime

Posted sssblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boost datetime相关的知识,希望对你有一定的参考价值。

To create a date, use the class boost::gregorian::date

1. date

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

int main()

  boost::gregorian::date d(2014, 1, 31);
  std::cout << d.year() << std::endl;
  std::cout << d.month() << std::endl;
  std::cout << d.day() << std::endl;
  std::cout << d.day_of_week() << std::endl;
  std::cout << d.end_of_month() << std::endl;

  date d = day_clock::universal_day();
  std::cout << d.year() << std::endl;
  std::cout << d.month() << std::endl;
  std::cout << d.day() << std::endl‘;

  d = date_from_iso_string("20140131");
  std::cout << d.year() << std::endl;
  std::cout << d.month() << std::endl;
  std::cout << d.day() << std::endl;
  return 0;

boost::gregorian::date provides several constructors to create dates. The most basic constructor takes a year, a month, and a day as parameters.

boost::gregorian::date returns the current date. The member function universal_day() returns a UTC date, which is independent of time zones and daylight savings. boost::gregorian::day_clock also provides a member function called local_day(), which takes local settings into account. To retrieve the current date within the local time zone, use local_day().

date_from_iso_string converts a date in the ISO 8601 format. Other functions include: boost::gregorian::from_simple_string() and boost::gregorian::from_us_string().

2. date_duration

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

using namespace boost::gregorian;

int main()

  date d1(2014, 1, 31);
  date d2(2014, 2, 28);
  date_duration dd = d2 - d1;
  std::cout << dd.days() << std::endl;

date_duration dd4;
std::cout << dd.days() << std::endl;
weeks ws4;
std::cout << ws.days() << std::endl;
months ms4;
std::cout << ms.number_of_months() << std::endl;
years ys4;
std::cout << ys.number_of_years() << std::endl;
return 0;

boost::gregorian::date overloads operator-, two points in time can be subtracted. The return value is of type boost::gregorian::date_duration and marks the duration between the two dates. days() returns the number of days in the duration specified.

Objects of type boost::gregorian::date_duration can also be created by passing the number of days as a single parameter to the constructor. To create a duration that involves weeks, months, or years, use boost::gregorian::weeks, boost::gregorian::months, or boost::gregorian::years

3. date_period

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

using namespace boost::gregorian;

int main()

  date d1(2014, 1, 1);
  date d2(2014, 2, 28);
  date_period dpd1, d2;
  date_duration dd = dp.length();
  std::cout << dd.days() << std::endl;

  std::cout.setf(std::ios::boolalpha);
  std::cout << dp.contains(d1) << std::endl;
  std::cout << dp.contains(d2) << std::endl;
  return 0;

The constructor of boost::gregorian::date_period can accept two kinds of input. You can pass two parameters of type boost::gergorian::date, one for the beginning date and one for the end date. Please note that the day before the end date is actually that last day of the period.

dp contains d1 while dose not contain d2.

4. iterator

#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

using namespace boost;

int main()

  gregorian::date d(2014, 5, 12);
  gregorian::day_iterator itd;
  std::cout << *++it << std::endl;
  std::cout << date_time::next_weekday(*it, gregorian::greg_weekday(date_time::Friday)) << std::endl;
return 0;

Use the iterator boost::gregorian::day_iterator to jump forward or backward by a day from a specific date. Use boost::gregorian::week_iterator, boost::gregorian::month_iterator, and boost::gregorian::year_iterator to jump by weeks, months, or years, respectively.

以上是关于boost datetime的主要内容,如果未能解决你的问题,请参考以下文章

Joda Time使用小计

C# 计算两个日期的时间间隔

.net 计算当前时间距离今晚00:00:00还有多少分多少秒

Boost总结汇总

Boost.Thread 链接 - boost_thread 与 boost_thread-mt

Boost 和 Boost.Build 的设置