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_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;
}
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)
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)
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)
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)。
在 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)