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 use binary regex in Rust?
- How to map a Rust slice?
- How to compare two Rust HashMaps?
- How to yield a thread in Rust?
- How to make regex case insensitive in Rust?
- How to use regex to match a group in Rust?
- How to match whitespace with a regex in Rust?
- How to use regex lookbehind in Rust?
- How to match digits with regex in Rust?
- How to use regex to match a double quote in Rust?
See more codes...