-
Notifications
You must be signed in to change notification settings - Fork 1
3.1 finding today's date
JeffGarland edited this page May 14, 2013
·
3 revisions
You need to find the year, month day values for today's date.
Using Boost date-time this can be easily achieved.
//get easy access to the needed types
#include <boost/date_time.hpp>
using boost::gregorian::date;
using boost::gregorian::day_clock;
using boost::gregorian::greg_year_month_day;
using std::cout;
using std::endl;
//read the clock with local time adjustment according to computer settings
date d = day_clock::local_day();
//extract the year month day values
date::ymd_type ymd = d.year_month_day();
After extracting the year month day the information can be printed. Note that the month is a special type in the ymd_type that will print as a string -- so the following will print something like 2013-May-13
cout << "The current date is :" << ymd.year << "-"
<< ymd.month << "-" << ymd.day << endl;
If you want the month as a number then call as_number on the month
cout << "The current date is :" << ymd.year << "-"
<< ymd.month.as_number() << "-" << ymd.day << endl;
In boost date-time you can also use local based i/o on the date type directly
cout.imbue(std::locale(cout.getloc(), new boost::gregorian::date_facet("%Y %m %d")));
cout << "The current date using faceted date output:" << d << endl; //2013 05 13