c++ 当前日期和时间字符串

yzxexxkh  于 2023-03-25  发布在  其他
关注(0)|答案(7)|浏览(206)

我写了一个函数来获取当前日期和时间的格式: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;
}
omqzjyyz

omqzjyyz1#

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

#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_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;
}
c7rzv4ha

c7rzv4ha2#

非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;
}
8ulbf1ek

8ulbf1ek3#

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

time_t _tm =time(NULL );

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

样本输出:

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

toiithl64#

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

产出

13-12-2021 09:24:44

它可以在Visual Studio 2019(v16.11.5)中使用最新的 C++ 版本(/std:c++latest)。

2uluyalo

2uluyalo5#

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

mepcadol6#

#include <chrono>
#include <iostream>

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

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

fafcakar7#

我想使用C11的答案,但我不能,因为GCC 4.9不支持std::put_time。
std::put_time implementation status in GCC?
我最终使用了一些C
11来稍微改进非C11的答案。对于那些不能使用GCC 5,但仍然希望使用C11的日期/时间格式的人:

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());

相关问题