相关疑难解决方法(0)

当前日期和时间为字符串

我写了一个函数来获取格式的当前日期和时间: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; …
Run Code Online (Sandbox Code Playgroud)

c++ string time datetime date

63
推荐指数
6
解决办法
19万
查看次数

如何格式化日期时间对象格式为dd/mm/yyyy?

如何使用Boost库以dd/mm/yyyy H格式打印当前日期?

是)我有的:

boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
cout << boost::posix_time::to_simple_string(now).c_str();

2009-Dec-14 23:31:40
Run Code Online (Sandbox Code Playgroud)

但我想要:

2009年12月14日23:31:40

c++ boost date

47
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×2

date ×2

boost ×1

datetime ×1

string ×1

time ×1