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
chronocrate 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 regex to match a double quote in Rust?
- How to replace strings using Rust regex?
- How to use non-capturing groups in Rust regex?
- Word boundary example in regex in Rust
- How to use regex to match a group in Rust?
- Example of struct private field in Rust
- How to multiply matrices in Rust?
- How to parse JSON string in Rust?
- How to initialize a Rust HashMap?
See more codes...