Here is a simple C++ program that displays the current system time using the C++ standard library (<ctime>
and <iostream>
):
Example Output:
Explanation:
-
time(0)
gets the current time as seconds since the Epoch (January 1, 1970). -
localtime()
converts that to atm
structure containing local time info. -
tm_hour
,tm_min
, andtm_sec
give hours, minutes, and seconds.
Let me know if you want it in 12-hour format, with AM/PM, or using C++20 <chrono>
instead.
1. Current Time with Date and AM/PM Format
✅ 2. Using C++20 <chrono>
Library (Modern Approach)
⚠️ Requires C++20 standard compiler support.
✅ 3. Realtime Clock Refreshing Every Second (Like a Clock)
🔧 To Compile and Run:
-
Linux/macOS (g++):
-
Windows (g++ or Dev C++):
-
Replace
system("clear")
withsystem("cls")
for clearing the screen.
-