DEV Community

Sivakumar
Sivakumar

Posted on • Edited 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!!!

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

nextjs tutorial video

📺 Youtube Tutorial Series

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️