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   Sanny.Liu-CV&&ML  阅读(738)  评论(0编辑  收藏  举报

(评论功能已被禁用)
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示