Skip to content

How do I get the Month as a number? #550

Closed Answered by Bas-Man
Bas-Man asked this question in Question
Discussion options

You must be logged in to vote

The answer is to use as u8 because the Month enum has #[repr(u8)]

So one can simply do

use time::OffsetDateTime;

fn main() {
    let date = OffsetDateTime::now_utc().date();
    println!("{}", date);
    let year = date.month();
    let mon = date.month() as u8;
    println!("{} {}", year, mon);
    // try using the `time` crate here
}

Playground

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jhpratt
Comment options

Answer selected by Bas-Man
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants