How to localize date format in C

3 Answers

0 votes
#include <stdio.h>
#include <time.h>
#include <locale.h>

// locale‑aware date formatting

int main() {
    // Use the system default locale
    setlocale(LC_TIME, "");

    time_t t = time(NULL);
    struct tm *tm_info = localtime(&t);

    char buffer[128];
    strftime(buffer, sizeof(buffer), "%x", tm_info);  // %x = localized date

    printf("Localized date: %s\n", buffer);
    
    return 0;
}



/*
run:

Localized date: 04/19/2026

*/

 



answered 17 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <locale.h>

// Forcing a specific locale

int main() {
    // Forcing a specific locale
    setlocale(LC_TIME, "en_US.utf8");

    time_t t = time(NULL);
    struct tm *tm_info = localtime(&t);

    char buffer[128];
    strftime(buffer, sizeof(buffer), "%x", tm_info);  // %x = localized date

    printf("Localized date: %s\n", buffer);
    
    return 0;
}



/*
run:

Localized date: 04/19/2026

*/

 



answered 17 hours ago by avibootz
0 votes
#include <stdio.h>
#include <time.h>
#include <locale.h>

// Forcing a specific locale

int main() {
    // Forcing a specific locale
    setlocale(LC_TIME, "en_US.utf8");

    time_t t = time(NULL);
    struct tm *tm_info = localtime(&t);

    char buffer[128];
    strftime(buffer, sizeof(buffer), "%A, %x", tm_info); // %A Full weekday name

    printf("Localized date: %s\n", buffer);
    
    return 0;
}



/*
run:

Localized date: Sunday, 04/19/2026

*/

 



answered 17 hours ago by avibootz
...