当前日期和时间为字符串

Kat*_*tie 63 c++ string time datetime date

我写了一个函数来获取格式的当前日期和时间: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;
}
Run Code Online (Sandbox Code Playgroud)

awe*_*oon 140

由于C++ 11可以使用std::put_timeiomanip标题:

#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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

  • gcc 4.9中仍然缺少`std :: put_time`. (35认同)
  • gcc 5.0有它,但不是早期版本. (5认同)

Max*_*Max 108

非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;
}
Run Code Online (Sandbox Code Playgroud)

  • 对于任何给定的格式字符串来说,计算最大可能的字符串长度并不需要太多考虑. (4认同)
  • 如果它溢出缓冲区怎么办?我想应该有一个'strftime`的返回值,你需要在实际代码中检查没有? (3认同)

bir*_*sht 23

你可以使用time.h的asctime()函数来简单地得到一个字符串.

time_t _tm =time(NULL );

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

样本输出:

The current date/time is:Fri Oct 16 13:37:30 2015
Run Code Online (Sandbox Code Playgroud)

  • 如果它没有提供想要或需要的东西,那么简洁明了的代码就无济于事.想要一辆卡车(绳子),你得到一辆.想要运输液体,你需要一辆油罐车,并且不会很高兴得到一辆自卸车...... (4认同)
  • 不,你不能:asctime不提供问题中要求的日期/时间格式! (3认同)
  • 很好的答案,但asctime()有一个非常糟糕的想法,返回一个LF终止字符串... (3认同)
  • @Aconcagua虽然没有,它确实给你一个字符串的时间.代码简洁明了. (2认同)

Syn*_*nck 20

对于 C++20,时间点格式(字符串)可在 (chrono) 标准库中使用。 https://en.cppreference.com/w/cpp/chrono/system_clock/formatter

#include <chrono>
#include <format>
#include <iostream>

int main()
{
   const auto now = std::chrono::system_clock::now();
   std::cout << std::format("{:%d-%m-%Y %H:%M:%OS}", now) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出

13-12-2021 09:24:44
Run Code Online (Sandbox Code Playgroud)

它适用于具有最新 C++ 语言版本 (/std:c++latest) 的 Visual Studio 2019 (v16.11.5)。


Bla*_*ack 7

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

#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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

#include <chrono>
#include <iostream>

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

    std::cout << cc << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢您提供此代码片段,它可能会提供一些有限的即时帮助。[正确的解释](https://meta.stackexchange.com/q/114762/349538) 将通过展示为什么这是一个很好的问题解决方案来极大地提高其长期价值,并将使其对未来的读者更有用与其他类似的问题。请[编辑]您的答案以添加一些解释,包括您所做的假设,例如`#include`s。 (3认同)
  • 这实际上返回了什么? (2认同)