当前日期和时间为字符串

Current date and time as string

本文关键字:字符串 时间 当前日期      更新时间:2023-10-16

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

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

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

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

您可以使用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

在c++ 20中,(chrono)标准库中提供了时间点格式化(to string)。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)。

在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;
}
#include <chrono>
#include <iostream>
int main()
{
    std::time_t ct = std::time(0);
    char* cc = ctime(&ct);
    std::cout << cc << std::endl;
    return 0;
}

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

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

我最终使用了一些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());