![](/img/trans.png)
[英]How to get std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> from variables of year,month,day,hour,minute and seconds?
[英]Show day, month and year with std::chrono?
我读到我可以用std::chrono
显示当前的日,月和年但我怎么能这样做?
// Example program
#include <iostream>
#include <string>
#include <chrono>
int main()
{
using namespace std::chrono;
cout << std::chrono::day;
}
我做这个代码,但它不起作用,我总是收到这个
error: 'day' is not a member of 'std::chrono
我做错了什么?
std::put_time
是你需要的:
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&now), "%Y-%m-%d") << "\n";
}
打印:
2018年5月14日
基于std:strftime的不同方法:
#include <iomanip>
#include <ctime>
#include <chrono>
#include <iostream>
int main()
{
auto now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm ptm;
localtime_s(&ptm, &now_c);
char buffer[80];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S ", &ptm);
std::cout << buffer;
}
结果:
2018-05-14 19:33:11
( localtime_s
在std命名空间之外并使用稍微不同的接口)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.