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
- How to get time from milliseconds in Rust
- What type to use for datetime in Rust
- How to sleep in Rust
- How to get execution time in Rust
- Using now to get current time in Rust
- How to get current date in Rust
- How to add second to time in Rust
- How to add day to date in Rust
- How to convert timestamp to datetime in Rust
- How to convert datetime to timestamp in Rust
More of Rust
- How to convert a Rust HashMap to a BTreeMap?
- How to replace all matches using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to use regex to match a group in Rust?
- How to get a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to use a tuple as a key in a Rust HashMap?
- How to parse JSON string in Rust?
- How to use regex with bytes in Rust?
- How to calculate the inverse of a matrix in Rust?
See more codes...