当前日期和时间为字符串
Current date and time as string
我写了一个函数来获取当前日期和时间的格式: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_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;
}
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());
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 通过字符串来计算长度需要更长的时间,而不是移动字符串几次?
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- 如何将时间字符串 (M:SS) 转换为浮点数
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 时间持续时间到时间字符串
- 如何将我的每个矢量字符串转换为纪元时间日期
- 如何找到修改后的字符串与原始字符串相等的时间
- 在C++中修改字符串的BigO时间复杂度是多少
- 格式化的时间字符串在解析为time_point时会产生小时差异
- 使用 ctime 保存新时间点会覆盖旧字符串?
- 将日期字符串转换为 unix 时间戳
- 有序地图字符串搜索与整数搜索之间的时间复杂度
- 字符串连接时间复杂度 c++
- 提升,从字符串问题中获得时间
- C 将字符串时间戳转换为STD :: Chrono :: System_Clock :: Time_point
- 将字符串时间转换为UNIX时间戳
- 最接近地图的字符串/时间戳