Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) GetToday

 

GetToday is a time code snippet to obtain today's date.

 

 

 

 

 

GetToday using STL only

 

#include <ctime>
#include <iostream>

int main ()
{
  std::time_t my_time;
  std::time( &my_time );
  const std::tm * const time_and_date = std::localtime(&my_time);
  std::cout << std::asctime(time_and_date);
}

 

Today's screen output:

 

Sun Aug 8 09:01:41 2010

 

 

 

 

 

GetToday using Boost.Date_Time

 

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

int main()
{
  const boost::gregorian::date today
    = boost::gregorian::day_clock::local_day();
  std::cout
    << today.day_of_week() << " "
    << today.month() << " "
    << today.day() << " "
    << today.year() << '\n';
  std::cout
    << boost::gregorian::to_simple_string(today) << '\n'
    << boost::gregorian::to_iso_extended_string(today) << '\n'
    << boost::gregorian::to_iso_string(today) << '\n'
    << boost::gregorian::to_sql_string(today) << '\n';
}

 

Today's screen output:

 

Sun Aug 8 2010
2010-Aug-08
2010-08-08
20100808
2010-08-08

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict