C++中显示当前时间的几种方法:使用 time() 获取时间戳使用 std::chrono 类获取系统时间使用第三方库(如 Boost.Date_Time)
如何在 C++ 中显示当前时间
在 C++ 中显示当前时间的方法有几种:
1. 使用标准库函数 time()
#include <iostream> #include <ctime> int main() { time_t now = time(0); std::cout << "Current time: " << ctime(&now); return 0; }
2. 使用类 std::chrono
立即学习“C++免费学习笔记(深入)”;
#include <iostream> #include <chrono> int main() { auto now = std::chrono::system_clock::now(); auto time_t_now = std::chrono::system_clock::to_time_t(now); std::cout << "Current time: " << ctime(&time_t_now); return 0; }
3. 使用第三方库
有许多第三方库可以帮助你显示当前时间,例如 Boost.Date_Time:
#include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); std::cout << "Current time: " << now; return 0; }
以上就是c++++如何显示当前时间的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号