rustHow to format time in Rust
Rust provides a number of ways to format time.
The most common way is to use the chrono
crate. This crate provides a number of types and functions to work with dates and times.
Example code
use chrono::{DateTime, Utc};
let now: DateTime<Utc> = Utc::now();
println!("{}", now.format("%Y-%m-%d %H:%M:%S"));
Output example
2020-09-17 11:45:00
The code above uses the chrono
crate to get the current time in UTC and then formats it using the format
function. The format string %Y-%m-%d %H:%M:%S
specifies the format of the output.
The chrono
crate also provides other types and functions to work with dates and times, such as DateTime
and Duration
.
Helpful links
Related
More of Rust
- How to replace a capture group using Rust regex?
- How to parse a file with Rust regex?
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to use named capture groups in Rust regex?
- How to replace all matches using Rust regex?
- How to use Unicode in a regex in Rust?
- How to replace strings using Rust regex?
- Regex example to match multiline string in Rust?
- How to extract data with regex in Rust?
See more codes...