How to get current year and month from Chrono::DateTime?

Really... How to get the year, month, and date component from Chrono::DateTime ? Even the documentation doesn't say anything about this topic.

Do I need to convert it into Date<Tz> type ? but once again there is no function to get the year component from it.

let current_date = chrono::Utc::now();
let year = current_date.year();  //this is not working
let month = current_date.month();
let date = current_date.date();

year(), month() and day() are part of the chrono::Datelike trait and you have to import the trait with use chrono::Datelike; before you can use the methods.

use chrono::Datelike;

fn main() {
    let current_date = chrono::Utc::now();
    let year = current_date.year();
    let month = current_date.month();
    let day = current_date.day();
    println!("{}-{}-{}", year, month, day);
}
2 Likes