fork download
  1. #include <iostream>
  2. #include <iomanip> // Required for std::put_time
  3. #include <ctime> // Required for std::tm and std::localtime
  4.  
  5. int main() {
  6. std::time_t t = std::time(nullptr); // Get current time
  7. std::tm tm_struct = *std::localtime(&t); // Convert to local time structure
  8.  
  9. // Format and output the date and time
  10. std::cout << "Current date and time: " << std::put_time(&tm_struct, "%Y-%m-%d %H:%M:%S") << std::endl;
  11.  
  12. // Example with a different format
  13. std::cout << "Formatted date: " << std::put_time(&tm_struct, "%d.%m.%Y | %H:%M") << std::endl;
  14.  
  15. return 0;
  16. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Current date and time: 2025-08-20 18:33:44
Formatted date: 20.08.2025 | 18:33