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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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"));
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("%+"));
All the code examples can be found in this link
Please feel free to share your feedback.
Happy reading!!!
Top comments (0)