How to display the current time in c++

下次还敢
Release: 2024-04-22 17:51:50
Original
359 people have browsed it

Several methods to display the current time in C: use time() to obtain the timestamp, use the std::chrono class to obtain the system time, use a third-party library (such as Boost.Date_Time)

How to display the current time in c++

How to display the current time in C

There are several ways to display the current time in C:

1. Use the standard library functiontime()

#include  #include  int main() { time_t now = time(0); std::cout << "Current time: " << ctime(&now); return 0; }
Copy after login

2. Use the classstd::chrono

#include  #include  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; }
Copy after login

3. Use third-party libraries

There are many third-party libraries that can help you display the current time, such as Boost.Date_Time:

#include  #include  int main() { boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); std::cout << "Current time: " << now; return 0; }
Copy after login

The above is the detailed content of How to display the current time in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!