rustHow to format date string in Rust
Formatting date strings in Rust can be done using the chrono
crate. This crate provides a wide range of formatting options for dates and times.
Below is an example of how to format a date string in Rust:
use chrono::{DateTime, Utc};
let now: DateTime<Utc> = Utc::now();
let formatted_date = now.format("%Y-%m-%d %H:%M:%S").to_string();
println!("Formatted date: {}", formatted_date);
Output
Formatted date: 2020-09-17 13:45:12
Explanation:
- The
chrono
crate is imported withuse chrono::{DateTime, Utc};
- The current date and time is obtained with
let now: DateTime<Utc> = Utc::now();
- The date is formatted with
let formatted_date = now.format("%Y-%m-%d %H:%M:%S").to_string();
- The formatted date is printed with
println!("Formatted date: {}", formatted_date);
Helpful links:
More of Rust
- How to get a capture group using Rust regex?
- How to use Unicode in a regex in Rust?
- How to get the last element of a Rust slice?
- How to match the end of a line in a Rust regex?
- How to replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use an enum in a Rust HashMap?
- How to match whitespace with a regex in Rust?
- How to split a string with Rust regex?
- How to match a URL with a regex in Rust?
See more codes...