该<chrono>库只处理时间,而不处理日期,除了system_clock可以将其时间点转换为time_t。因此,使用<chrono>日期不会有太大改善。希望我们能chrono::date在不久的将来得到类似的东西。
也就是说,您可以通过<chrono>以下方式使用:
#include <chrono>  // chrono::system_clock
#include <ctime>   // localtime
#include <sstream> // stringstream
#include <iomanip> // put_time
#include <string>  // string
std::string return_current_time_and_date()
{
    auto now = std::chrono::system_clock::now();
    auto in_time_t = std::chrono::system_clock::to_time_t(now);
    std::stringstream ss;
    ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
    return ss.str();
}
请注意,这std::localtime可能会导致数据争用。localtime_r或类似的功能可能在您的平台上可用。
更新:
使用Howard Hinnant的日期库的新版本,您可以编写:
#include "date.h"
#include <chrono>
#include <string>
#include <sstream>
std::string return_current_time_and_date() {
  auto now = std::chrono::system_clock::now();
  auto today = date::floor<days>(now);
  std::stringstream ss;
  ss << today << ' ' << date::make_time(now - today) << " UTC";
  return ss.str();
}
这将打印出类似“ 2015-07-24 05:15:34.043473124 UTC”的内容。
无关紧要的是,const在C ++ 11中,返回对象已变得不可取。const返回值不能移出。我还删除了尾随const,因为尾随const仅对成员函数有效,并且此函数无需成为成员。