如何使用格式为dd / mm / yyyy格式的日期时间对象呢? [英] How to format date time object with format dd/mm/yyyy?

查看:241
本文介绍了如何使用格式为dd / mm / yyyy格式的日期时间对象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能打印当前的日期,使用升压库,格式DD / MM / YYYY H +

我有什么:

 的boost ::了posix_time ::现在的ptime =的boost ::了posix_time :: second_clock :: LOCAL_TIME();
COUT<<提高::了posix_time :: to_simple_string(现在的).c_str();2009年 - 12月14 23点31分四十秒

不过,我想:


  

14日 - 12月2009年23点31分40秒。



解决方案

如果你使用的Boost.Date_Time 时,这是使用的IO面完成。

您需要包括升压/ DATE_TIME /了posix_time / posix_time_io.hpp 来得到正确的typedef面( wtime_facet time_facet 等)的boost ::了posix_time ::分组时间。一旦做到这一点,在code是pretty简单。你叫灌输在的ostream 要输出到,那么就输出你的分组时间

 的#include<&iostream的GT;
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time.hpp>
#包括LT&;升压/ DATE_TIME /了posix_time / posix_time_io.hpp>使用空间boost ::了posix_time;
使用命名空间std;INT主(INT ARGC,字符** argv的){
  time_facet *面=新time_facet(%D-%B-%Y%H:%M:%S);
  cout.imbue(区域设置(cout.getloc(),面));
  COUT<< second_clock :: LOCAL_TIME()&所述;&下; ENDL;
}

输出:

  12月14日 -  2009年16点十三分14秒

又见<一个href=\"http://www.boost.org/doc/libs/1%5F35%5F0/doc/html/date%5Ftime/date%5Ftime%5Fio.html#date%5Ftime.format%5Fflags\">list格式的旗帜在升压文档,如果你想输出的东西票友。

How could I print the current date, using Boost libraries, in the format dd/mm/yyyy H?

What I have:

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

But I want:

14-Dec-2009 23:31:40

解决方案

If you're using Boost.Date_Time, this is done using IO facets.

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;
}

Output:

14-Dec-2009 16:13:14

See also the list of format flags in the boost docs, in case you want to output something fancier.

这篇关于如何使用格式为dd / mm / yyyy格式的日期时间对象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆