首页 > 解决方案 > 如何从 Chrono::DateTime 中获取年月日组件?

问题描述

该文档没有说明有关此主题的任何内容。我需要把它转换成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

标签: rustrust-chrono

解决方案


你需要DateLiketrait 并使用它的方法。检索Date要对其进行操作的组件:

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

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

操场


推荐阅读