c++获取当前时间,并转化为string

//头文件
#include <iostream>
#include <sstream>
#include <iomanip>
#include <chrono>
 
using namespace std;
 
int main() {
    auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
 
    //转为字符串
    std::stringstream ss;
    // 可以分别以不同的形式进行显示
//    ss << std::put_time(std::localtime(&t), "%Y-%m-%d-%H-%M-%S");
    ss << std::put_time(std::localtime(&t), "%Y年%m月%d日%H时%M分%S秒");
//    ss << std::put_time(std::localtime(&t), "%Y%m%d%H%M%S");
 
    std::string str_time = ss.str();
 
//    在控制台输出当前时间
    cout <<  str_time << endl;
    cout<<"按任意键继续……";
//    以下代码是为了不闪屏退出
    cin.clear();
    cin.sync();
    cin.get();
    return 0;
}

输出结果:

2019年07月27日09时34分44秒
按任意键继续……

 

转载:https://blog.csdn.net/G_66_hero/article/details/97487543

posted on 2022-05-15 19:04  Sanny.Liu-CV&&ML  阅读(400)  评论(0编辑  收藏  举报

导航