Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::put_time formats

Tags:

c++

c++11

I want understand how to work std::put_time, and how can I get date stamp in "YYYY/MM/DD HH:MM:SS" format. Now I write somthing like this:

std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
        std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
        std::cout  << std::put_time(std::localtime(&now_c), "%F %T") << '\n';

and output is 2011-10-25 12:00:08, how can I get the date as 2011/10/25 12:00:08.

like image 554
Topilski Alexandr Avatar asked May 19 '26 20:05

Topilski Alexandr


1 Answers

As mentioned 1 hour ago here, cppreference has good documentation on this: http://en.cppreference.com/w/cpp/io/manip/put_time

Specifically, you can get the format you described using the following format string:

std::cout  << std::put_time(std::localtime(&now_c), "%Y/%m/%d %T")
like image 96
jogojapan Avatar answered May 21 '26 10:05

jogojapan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!