如何格式化dd / mm / yyyy格式的日期时间对象? [英] How to format date time object with format dd/mm/yyyy?
问题描述
如何使用 Boost 库打印当前日期格式dd / mm / yyyy H?
How could I print the current date, using Boost libraries, in the format dd/mm/yyyy H?
我有什么:
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
cout << boost::posix_time::to_simple_string(now).c_str();
2009-Dec-14 23:31:40
但是我想:
2009年12月14日23:31:40
14-Dec-2009 23:31:40
推荐答案
如果您使用 Boost.Date_Time ,这是使用IO facet完成的。
If you're using Boost.Date_Time, this is done using IO facets.
您需要包含 boost / date_time / posix_time / posix_time_io.hpp
以获取正确的facet typedefs( wtime_facet
, time_facet
等) boost :: posix_time :: ptime
。一旦完成,代码很简单。您要在要输出的 ostream
上调用imbue,然后输出您的 ptime
:
You need to include boost/date_time/posix_time/posix_time_io.hpp
to get the correct facet typedefs (wtime_facet
, time_facet
, etc.) for boost::posix_time::ptime
. Once this is done, the code is pretty simple. You call imbue on the ostream
you want to output to, then just output your ptime
:
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
using namespace boost::posix_time;
using namespace std;
int main(int argc, char **argv) {
time_facet *facet = new time_facet("%d-%b-%Y %H:%M:%S");
cout.imbue(locale(cout.getloc(), facet));
cout << second_clock::local_time() << endl;
}
输出:
14-Dec-2009 16:13:14
格式标志列表在boost文档中,以防你想输出一些喜好者。
See also the list of format flags in the boost docs, in case you want to output something fancier.
这篇关于如何格式化dd / mm / yyyy格式的日期时间对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!