rustHow to format datetime in Rust
Rust provides a powerful library for formatting and manipulating dates and times called chrono
. It is a feature-rich library that supports a wide range of operations.
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
library to get the current date and time in UTC and then formats it using the format
method. The format string %Y-%m-%d %H:%M:%S
specifies the desired output format.
Parts of the code:
use chrono::{DateTime, Utc};
: imports theDateTime
andUtc
types from thechrono
library.let now: DateTime<Utc> = Utc::now();
: creates aDateTime
object representing the current date and time in UTC.println!("{}", now.format("%Y-%m-%d %H:%M:%S"));
: prints theDateTime
object in the specified format.
Helpful links
Related
More of Rust
- How to match a URL with a regex in Rust?
- How to replace a capture group using Rust regex?
- How to clear a Rust HashMap?
- Yield example in Rust
- Example of yield_now in Rust?
- How to convert a Rust slice to a fixed array?
- How to use regex to match a group in Rust?
- How to convert a Rust HashMap to a BTreeMap?
- How to yield a thread in Rust?
- How to replace strings using Rust regex?
See more codes...