如何在 C++ 中格式化日期和时间字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10289017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:52:18  来源:igfitidea点击:

How to format date and time string in C++

c++datetimemfc

提问by Tae-Sung Shin

Let's say I have time_t and tm structure. I can't use Boost but MFC. How can I make it a string like following?

假设我有 time_t 和 tm 结构。我不能使用 Boost 而是 MFC。我怎样才能使它成为如下所示的字符串?

Mon Apr 23 17:48:14 2012

Is using sprintf the only way?

使用 sprintf 是唯一的方法吗?

回答by Jerry Coffin

The C library includes strftimespecifically for formatting dates/times. The format you're asking for seems to correspond to something like this:

C 库包括strftime专门用于格式化日期/时间。您要求的格式似乎对应于这样的内容:

char buffer[256];

strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &your_tm);

I believe std::put_timeuses a similar format string, though it does relieve you of having to explicitly deal with a buffer. If you want to write the output to a stream, it's quite convenient, but to get it into a string it's not a lot of help -- you'd have to do something like:

我相信std::put_time使用类似的格式字符串,尽管它确实让您不必显式处理缓冲区。如果您想将输出写入流,这非常方便,但要将其放入字符串中并没有太大帮助——您必须执行以下操作:

std::stringstream buffer;

buffer << std::put_time(&your_tm, "%a %b %d %H:%M:%S %Y");

// now the result is in `buffer.str()`.

std::put_timeis new with C++11, but C++03 has a time_putfacet in a locale that cando the same thing. If memory serves, I did manage to make it work once, but after that decided it wasn't worth the trouble, and I haven't done it since.

std::put_time是 C++11 的新特性,但 C++03time_put在语言环境中有一个方面可以做同样的事情。如果没记错的话,我确实设法让它工作了一次,但在那之后决定不值得麻烦,从那以后我就没有这样做过。

回答by Kevin Anderson

I'd try std::put_time. See the link herefor information on how to use it. It supports full format strings and such.

我会尝试std::put_time。有关如何使用它的信息,请参阅此处链接。它支持全格式字符串等。

回答by Alexey Frunze

ctime()produces strings in that format. It takes a pointer to a time_t.
There's also asctime()that takes a pointer to a struct tmand does the same.

ctime()生成该格式的字符串。它需要一个指向 a 的指针time_t
asctime()有一个指向 a 的指针struct tm并执行相同的操作。

回答by GilesDMiddleton

If you need to worry about formatting on different locales, don't forget to initialize the CRT with the current locale. This affects COleDateTime too.

如果您需要担心不同语言环境的格式设置,请不要忘记使用当前语言环境初始化 CRT。这也会影响 COleDateTime。

setlocale(LC_COLLATE,“.OCP”); // sets the sort order

setlocale(LC_MONETARY, “.OCP”); // sets the currency formatting rules

setlocale(LC_NUMERIC, “.OCP”); // sets the formatting of numerals

setlocale(LC_TIME, “.OCP”); // defines the date/time formatting

See my blog post which ties in MSDN articles and other sources. http://gilesey.wordpress.com/2012/12/30/initailizing-mfccrt-for-consumption-of-regional-settings-internationalizationc

请参阅我的博客文章,该文章与 MSDN 文章和其他来源有关。 http://gilesey.wordpress.com/2012/12/30/initailizing-mfccrt-for-consumption-of-regional-settings-internationalizationc

回答by Jeeva

CTime obj1(time_tObj);

CString s = obj1.Format( "%a %b %d %H:%M:%S %Y" );

回答by Mark Ransom

MFC has COleDateTimewhich has a contructor that takes time_t(or __time64_t) and has a Formatmethod.

MFC 有COleDateTimewhich 有一个带time_t(or __time64_t)的构造函数并有一个Format方法。