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
- 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 replace a capture group using Rust regex?
- How to use non-capturing groups in Rust regex?
- How to split a string with Rust regex?
- How to use regex lookahead in Rust?
- How to parse a file with Rust regex?
- How to get a capture group using Rust regex?
- How to use groups in a Rust regex?
- How to implement a generator trait in Rust?
- How to drop box in Rust
- How to match the end of a line in a Rust regex?
See more codes...