How to localize date format in C++

2 Answers

0 votes
// format date using the user's locale

#include <iostream>
#include <locale>
#include <iomanip>
#include <ctime>

int main() {
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);

    // Use system default locale
    std::locale loc("");

    std::cout.imbue(loc);
    std::cout << std::put_time(&tm, "%x") << "\n";  // %x = localized date
}


/*
run:

04/19/26

*/

 



answered 18 hours ago by avibootz
0 votes
// date formatting with locale support // C++20

#include <chrono>
#include <format>
#include <iostream>
#include <locale>

int main() {
    auto now = std::chrono::system_clock::now();
    std::locale loc("en_US.utf8");

    std::cout.imbue(loc);
    std::cout << std::format(loc, "{:%x}", now) << "\n";
}



/*
run:

04/19/26

*/

 



answered 18 hours ago by avibootz
...