1

I am trying to convert boost::posix_time::ptime to YYMMDDHHMM format string. How to accomplish this?

Related: How to convert a boost::ptime to string

ssk
  • 9,045
  • 26
  • 96
  • 169

1 Answers1

1
#include <iostream>
#include <cstdlib>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
int main()
{
    std::string submitDateString        = "20190911T235959";
    boost::posix_time::ptime submitPtime = boost::posix_time::from_iso_string( submitDateString );

     // Use a facet to display time in a custom format (only hour and minutes).
    std::stringstream sstream;
    boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
    facet->format("%y%m%d%H%M");
    sstream.imbue(std::locale(std::locale::classic(), facet));
    sstream << submitPtime;

    std::cout << "submit date:" << sstream.str( ) << std::endl;
}
ssk
  • 9,045
  • 26
  • 96
  • 169