如何从Chrono::DateTime中获取年月日组成部分?

How to get the year, month, and date component from Chrono::DateTime?

The documentation 没有说任何关于这个话题。我需要将它转换成 Date<Tz> 吗?即使那样,也没有从中获取年份部分的功能。

let current_date = chrono::Utc::now();
let year = current_date.year();  //this is not working, it should output the current year with i32/usize type
let month = current_date.month();
let date = current_date.date();
no method named `month` found for struct `chrono::DateTime<chrono::Utc>` in the current scope

您需要 DateLike trait and use its methods。检索 Date 组件以对其进行操作:

use chrono::Datelike;
use chrono; // 0.4.19

fn main() {
    let current_date = chrono::Utc::now().date();
    println!("{}", current_date.year());
}

Playground