How to Format Dates and Times in Rust

Format dates and times in Rust using the chrono crate and format strings.

Use the chrono crate to format dates and times with the format! macro or the format method on DateTime objects.

use chrono::{DateTime, Utc};

fn main() {
    let now: DateTime<Utc> = Utc::now();
    let formatted = now.format("%Y-%m-%d %H:%M:%S").to_string();
    println!("{formatted}");
}