fork download
  1. #include <ctime>
  2. #include <iostream>
  3. #include <vector> // For dynamic buffer in a more robust solution
  4.  
  5. int main() {
  6. char buffer[80]; // Buffer to store the formatted time
  7. time_t rawtime;
  8. struct tm *timeinfo;
  9.  
  10. time(&rawtime); // Get current time
  11. timeinfo = localtime(&rawtime); // Convert to local time structure
  12.  
  13. // Format the time as "YYYY-MM-DD HH:MM:SS"
  14. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
  15. std::cout << "Current local time: " << buffer << std::endl;
  16.  
  17. // More complex formatting
  18. strftime(buffer, sizeof(buffer), "Today is %A, %B %d, %Y at %I:%M %p", timeinfo);
  19. std::cout << "Formatted time: " << buffer << std::endl;
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Current local time: 2025-08-20 17:43:44
Formatted time: Today is Wednesday, August 20, 2025 at 05:43 PM