Home > Blockchain >  Rust: how to remove fractional seconds from 2022-12-01T07:56:54.242352517Z wih chrono::offset::Utc::
Rust: how to remove fractional seconds from 2022-12-01T07:56:54.242352517Z wih chrono::offset::Utc::

Time:12-02

Hello I take the current time with chrono::offset::Utc::now()

but it returns this one:

2022-12-01T07:56:54.242352517Z

, I need it like this:

2022-12-01T07:56:54Z

. how can I do this?

CodePudding user response:

You can use to_rfc3339_opts. It accepts arguments for the format of the seconds and if the Z should be present.

let time = chrono::offset::Utc::now();
let formatted: String = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, true);

println!("{:?}", time);  // 2022-12-01T08:32:20.580242150Z
println!("{}", formatted);  // 2022-12-01T08:32:20Z

Rust Playground

  • Related