How to localize date format in Rust

1 Answer

0 votes
use chrono::prelude::*;

// French month names
const FRENCH_MONTHS: [&str; 12] = [
    "janvier", "février", "mars", "avril", "mai", "juin",
    "juillet", "août", "septembre", "octobre", "novembre", "décembre",
];

// French weekday names
const FRENCH_WEEKDAYS: [&str; 7] = [
    "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche",
];

fn main() {
    let now = Local::now();

    let weekday = FRENCH_WEEKDAYS[(now.weekday().num_days_from_monday()) as usize];
    let day = now.day();
    let month = FRENCH_MONTHS[(now.month() - 1) as usize];
    let year = now.year();
    let hour = now.hour();
    let minute = now.minute();

    println!("--- Localized Date (French, no ICU) ---");

    // Short date (dd/mm/yyyy)
    println!("Short date : {:02}/{:02}/{}", day, now.month(), year);

    // Long date (mardi 10 novembre 2009)
    println!("Long date  : {} {} {} {}", weekday, day, month, year);

    // Time (HH:MM)
    println!("Time       : {:02}:{:02}", hour, minute);

    // Weekday
    println!("Weekday    : {}", weekday);

    // Month name
    println!("Month name : {}", month);

    // Custom format
    println!(
        "Custom     : {} {} {} {} à {:02}:{:02}",
        weekday, day, month, year, hour, minute
    );
}



/*
run:

--- Localized Date (French, no ICU) ---
Short date : 19/04/2026
Long date  : dimanche 19 avril 2026
Time       : 17:34
Weekday    : dimanche
Month name : avril
Custom     : dimanche 19 avril 2026 à 17:34

*/

 



answered 6 hours ago by avibootz
...