C++ 当前日期和时间作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16357999/
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 20:17:26  来源:igfitidea点击:

Current date and time as string

c++stringdatedatetimetime

提问by Katie

I wrote a function to get a current date and time in format: DD-MM-YYYY HH:MM:SS. It works but let's say, its pretty ugly. How can I do exactly the same thingbut simpler?

我写了一个函数来获取当前日期和时间的格式:DD-MM-YYYY HH:MM:SS. 它有效,但让我们说,它非常丑陋。我怎样才能做完全相同但更简单的事情

string currentDateToString()
{
    time_t now = time(0);
    tm *ltm = localtime(&now);

    string dateString = "", tmp = "";
    tmp = numToString(ltm->tm_mday);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += "-";
    tmp = numToString(1 + ltm->tm_mon);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += "-";
    tmp = numToString(1900 + ltm->tm_year);
    dateString += tmp;
    dateString += " ";
    tmp = numToString(ltm->tm_hour);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += ":";
    tmp = numToString(1 + ltm->tm_min);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;
    dateString += ":";
    tmp = numToString(1 + ltm->tm_sec);
    if (tmp.length() == 1)
        tmp.insert(0, "0");
    dateString += tmp;

    return dateString;
}

回答by awesoon

Since C++11 you could use std::put_timefrom iomanipheader:

从 C++11 开始,您可以std::put_time从头iomanip文件中使用:

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);
    std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}

std::put_timeis a stream manipulator, therefore it could be used together with std::ostringstreamin order to convert the date to a string:

std::put_time是一个流操纵器,因此可以与它一起使用std::ostringstream以将日期转换为字符串:

#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>

int main()
{
    auto t = std::time(nullptr);
    auto tm = *std::localtime(&t);

    std::ostringstream oss;
    oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
    auto str = oss.str();

    std::cout << str << std::endl;
}

回答by Max

Non C++11 solution: With the <ctime>header, you could use strftime. Make sure your buffer is large enough, you wouldn't want to overrun it and wreak havoc later.

非 C++11 解决方案:使用<ctime>标头,您可以使用strftime. 确保您的缓冲区足够大,您不希望它溢出并在以后造成严重破坏。

#include <iostream>
#include <ctime>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer[80];

  time (&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
  std::string str(buffer);

  std::cout << str;

  return 0;
}

回答by birubisht

you can use asctime() function of time.h to get a string simply .

您可以使用 time.h 的 asctime() 函数来简单地获取字符串。

time_t _tm =time(NULL );

struct tm * curtime = localtime ( &_tm );
cout<<"The current date/time is:"<<asctime(curtime);

Sample output:

示例输出:

The current date/time is:Fri Oct 16 13:37:30 2015

回答by Black

Using C++ in MS Visual Studio 2015 (14), I use:

在 MS Visual Studio 2015 (14) 中使用 C++,我使用:

#include <chrono>

string NowToString()
{
  chrono::system_clock::time_point p = chrono::system_clock::now();
  time_t t = chrono::system_clock::to_time_t(p);
  char str[26];
  ctime_s(str, sizeof str, &t);
  return str;
}

回答by AGG GAA

#include <chrono>
#include <iostream>

int main()
{
    std::time_t ct = std::time(0);
    char* cc = ctime(&ct);

    std::cout << cc << std::endl;
    return 0;
}

回答by DiB

I wanted to use the C++11 answer, but I could not because GCC 4.9 does not support std::put_time.

我想使用 C++11 答案,但我不能,因为 GCC 4.9 不支持 std::put_time。

std::put_time implementation status in GCC?

GCC 中的 std::put_time 实现状态?

I ended up using some C++11 to slightly improve the non-C++11 answer. For those that can't use GCC 5, but would still like some C++11 in their date/time format:

我最终使用了一些 C++11 来稍微改进非 C++11 的答案。对于那些不能使用 GCC 5,但仍然想要一些 C++11 的日期/时间格式的人:

 std::array<char, 64> buffer;
 buffer.fill(0);
 time_t rawtime;
 time(&rawtime);
 const auto timeinfo = localtime(&rawtime);
 strftime(buffer.data(), sizeof(buffer), "%d-%m-%Y %H-%M-%S", timeinfo);
 std::string timeStr(buffer.data());