DEV Community

Sivakumar
Sivakumar

Posted on • Updated on

Rust Formatting: Date/Time

In this blog post, let us see how to format Date/Time using different specifiers

Formatting Date
Format date in mm/dd/yy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in mm/dd/yy format: {}", dt.format("%D"));
Enter fullscreen mode Exit fullscreen mode
Format date in yyyy-mm-dd format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in yyyy-mm-dd format: {}", dt.format("%F"));
Enter fullscreen mode Exit fullscreen mode
Format date in d-MMM-yyyy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in d-MMM-yyyy format: {}", dt.format("%v"));
Enter fullscreen mode Exit fullscreen mode
Format date in Day, dd-mm-yyyy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Day, dd-mm-yyyy format: {}", dt.format("%a, %d-%M-%Y"));
Enter fullscreen mode Exit fullscreen mode
Format date in Day, dd-mm-yyyy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Day, dd-mm-yyyy format: {}", dt.format("%A, %d-%M-%Y"));
Enter fullscreen mode Exit fullscreen mode
Format date in Full Week Day, dd-MMM-yyyy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Full Week Day, dd-MMM-yyyy format: {}", dt.format("%A, %d-%b-%Y"));
Enter fullscreen mode Exit fullscreen mode
Format date in Full Week Day, dd-MMMM-yyyy format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Full Week Day, dd-MMMM-yyyy format: {}", dt.format("%A, %d-%B-%Y"));
Enter fullscreen mode Exit fullscreen mode
Retrieve day of the year from Date
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Day of the year: {}", dt.format("%j"));
Enter fullscreen mode Exit fullscreen mode
Retrieve week day from Date
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Day of the year: {}, Week day (Starting with Sunday): {}", dt.format("%j"), dt.format("%u"));
    println!("Day of the year: {}, Week day (Starting with Monday): {}", dt.format("%j"), dt.format("%w"));
Enter fullscreen mode Exit fullscreen mode
Formatting Time
Time in 24-hour format HH:mm:ss
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 24-hour format HH:mm:ss: {}", dt.format("%H:%M:%S"));
Enter fullscreen mode Exit fullscreen mode
Time in 12-hour format HH:mm:ss
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss: {}", dt.format("%I:%M:%S"));
Enter fullscreen mode Exit fullscreen mode
Time in 12-hour format HH:mm:ss
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss <AM/PM>: {}", dt.format("%I:%M:%S %p"));
Enter fullscreen mode Exit fullscreen mode
Time in 12-hour format HH:mm:ss with timezone
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss <AM/PM> TZ: {}", dt.format("%I:%M:%S %p %:z"));
Enter fullscreen mode Exit fullscreen mode
Date and Time in ISO 8601/RFC 3339 format
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date and Time: {}", dt.format("%c"));
    println!("Date and Time in ISO 8601/RFC 3339 format: {}", dt.format("%+"));
Enter fullscreen mode Exit fullscreen mode

All the code examples can be found in this link

Please feel free to share your feedback.

Happy reading!!!

Top comments (0)