How to format a date using different formats in C++

2 Answers

0 votes
#include <iostream>
#include <ctime>
#include <iomanip>

/*
You can format dates in C++ using either the classic C‑style strftime()
or the modern C++20 <chrono> formatting.
*/

/*
Useful format codes:
%Y — 4‑digit year
%m — month (01–12)
%d — day of month
%H — hour (00–23)
%I — hour (01–12)
%M — minutes
%S — seconds
%A — full weekday name
%B — full month name
%c — locale’s default date/time
%j — day of year (001–366)
%W — week number (Mon-start)
%U — week number (Sun-start)
*/

int main() {
    time_t now = time(nullptr);          // Current timestamp
    tm* t = localtime(&now);             // Break into components

    char buf[128] = "";

    // --- Basic numeric formats ---
    strftime(buf, sizeof(buf), "%Y-%m-%d", t);
    std::cout << "[ISO 8601] YYYY-MM-DD: " << buf << "\n";

    strftime(buf, sizeof(buf), "%d/%m/%Y", t);
    std::cout << "[European] DD/MM/YYYY: " << buf << "\n";

    strftime(buf, sizeof(buf), "%m-%d-%Y", t);
    std::cout << "[US] MM-DD-YYYY: " << buf << "\n";

    // --- Time formats ---
    strftime(buf, sizeof(buf), "%H:%M:%S", t);
    std::cout << "[24-hour] HH:MM:SS: " << buf << "\n";

    strftime(buf, sizeof(buf), "%I:%M:%S %p", t);
    std::cout << "[12-hour] HH:MM:SS AM/PM: " << buf << "\n";

    // --- Full date with names ---
    strftime(buf, sizeof(buf), "%A, %B %d, %Y", t);
    std::cout << "Full weekday + month name: " << buf << "\n";

    strftime(buf, sizeof(buf), "%a, %b %d", t);
    std::cout << "Short weekday + month name: " << buf << "\n";

    // --- Combined date/time ---
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
    std::cout << "Full timestamp: " << buf << "\n";

    strftime(buf, sizeof(buf), "%c", t);
    std::cout << "Locale default date/time: " << buf << "\n";

    // --- Special formats ---
    strftime(buf, sizeof(buf), "Day of year: %j", t);
    std::cout << buf << "\n";

    strftime(buf, sizeof(buf), "Week number (Mon-start): %W", t);
    std::cout << buf << "\n";

    strftime(buf, sizeof(buf), "Week number (Sun-start): %U", t);
    std::cout << buf << "\n";
}



/*
run:

[ISO 8601] YYYY-MM-DD: 2026-05-20
[European] DD/MM/YYYY: 20/05/2026
[US] MM-DD-YYYY: 05-20-2026
[24-hour] HH:MM:SS: 09:17:23
[12-hour] HH:MM:SS AM/PM: 09:17:23 AM
Full weekday + month name: Wednesday, May 20, 2026
Short weekday + month name: Wed, May 20
Full timestamp: 2026-05-20 09:17:23
Locale default date/time: Wed May 20 09:17:23 2026
Day of year: 140
Week number (Mon-start): 20
Week number (Sun-start): 20

*/

 



answered May 20 by avibootz
edited May 20 by avibootz
0 votes
#include <chrono>
#include <format>
#include <iostream>


/*
complete, modern C++20 example that formats dates using 
std::chrono::system_clock + std::chrono::zoned_time + std::format.
*/

/*
Format Codes You Can Use
All of these work with std::format:
%Y — 4‑digit year
%m — month (01–12)
%d — day of month
%H — hour (00–23)
%I — hour (01–12)
%M — minutes
%S — seconds
%A — full weekday name
%B — full month name
%c — locale-style date/time
%j — day of year
%W — week number (Mon-start)
%U — week number (Sun-start)
*/


int main() {

    // Get current time from system clock
    auto now = std::chrono::system_clock::now();

    // Convert to a time_point representing local time
    // (uses your system's current timezone)
    std::chrono::zoned_time local_time{std::chrono::current_zone(), now};

    // Extract the local_time_point for formatting
    auto tp = local_time.get_local_time();

    // --- Print various formats using std::format ---

    // ISO 8601 date
    std::cout << "ISO date (YYYY-MM-DD): "
              << std::format("{:%Y-%m-%d}", tp) << "\n";

    // European format
    std::cout << "European (DD/MM/YYYY): "
              << std::format("{:%d/%m/%Y}", tp) << "\n";

    // US format
    std::cout << "US (MM-DD-YYYY): "
              << std::format("{:%m-%d-%Y}", tp) << "\n";

    // 24-hour time
    std::cout << "24-hour time: "
              << std::format("{:%H:%M:%S}", tp) << "\n";

    // 12-hour time
    std::cout << "12-hour time: "
              << std::format("{:%I:%M:%S %p}", tp) << "\n";

    // Full weekday + month name
    std::cout << "Full weekday + month: "
              << std::format("{:%A, %B %d, %Y}", tp) << "\n";

    // Short weekday + month
    std::cout << "Short weekday + month: "
              << std::format("{:%a, %b %d}", tp) << "\n";

    // Full timestamp
    std::cout << "Full timestamp: "
              << std::format("{:%Y-%m-%d %H:%M:%S}", tp) << "\n";

    // Locale-like representation
    std::cout << "Locale-style: "
              << std::format("{:%c}", tp) << "\n";

    // Day of year
    std::cout << "Day of year: "
              << std::format("{:%j}", tp) << "\n";

    // Week numbers
    std::cout << "Week number (Mon-start): "
              << std::format("{:%W}", tp) << "\n";

    std::cout << "Week number (Sun-start): "
              << std::format("{:%U}", tp) << "\n";
}




/*
run:

ISO date (YYYY-MM-DD): 2026-05-20
European (DD/MM/YYYY): 20/05/2026
US (MM-DD-YYYY): 05-20-2026
24-hour time: 09:28:04.403765162
12-hour time: 09:28:04.403765162 AM
Full weekday + month: Wednesday, May 20, 2026
Short weekday + month: Wed, May 20
Full timestamp: 2026-05-20 09:28:04.403765162
Locale-style: Wed May 20 09:28:04 2026
Day of year: 140
Week number (Mon-start): 20
Week number (Sun-start): 20

*/

 



answered May 20 by avibootz
...