Skip to content

3.6 day in a week month year or weeknumber

Jeff Trull edited this page May 15, 2013 · 6 revisions

Problem

Given a date in various formats, how can you calculate the week of the year, the day in the week or the day number in the year?

Solution

Once you create a Boost date object you can use its member functions to access the needed details.

#include <iostream>

#include "boost/date_time/gregorian/gregorian.hpp"

using namespace std;
using namespace boost::gregorian;

int main(int argc, char** argv)
{
    date cppNow(2013, May, 12);
    
    cout << "Date to test is "<< cppNow << "." << endl;
    cout << "Day of year: " << cppNow.day_of_year() << endl;
    cout << "ISO week number: " << cppNow.week_number() << endl;
    auto dow(cppNow.day_of_week());
    int dowNumber = dow;
    cout << "Day of week: " << dow << "(" << dowNumber << ")" << endl;
}
Clone this wiki locally