字符串形式的当前日期和时间

我编写了一个函数来获取格式为 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;
}
283703 次浏览

Since C++11 you could use std::put_time from iomanip header:

#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 is a stream manipulator, therefore it could be used together with std::ostringstream in order to convert the date to a string:

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

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

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

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.

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

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

std::put_time implementation status in GCC?

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:

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

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

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

With C++20, time point formatting (to string) is available in the (chrono) standard library. 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';
}

Output

13-12-2021 09:24:44

It works in Visual Studio 2019 (v16.11.5) with the latest C++ language version (/std:c++latest).