time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.12k stars 280 forks source link

Get Month as integer #504

Closed sedrik closed 2 years ago

sedrik commented 2 years ago

I might be overlooking something. But given a OffsetDateTime I can't find a way (except formatting) to get the numeric value for a month.

https://docs.rs/time/0.3.14/time/struct.OffsetDateTime.html#method.month returns a Month which doesn't seem to expose a numeric representation that I can find.

I would like to avoid formatting as that returns a Result, currently I have my own function that converts a Month into it's numeric value but it would be nice if the time library exposed this directly.

jhpratt commented 2 years ago

There's two ways to do this: impl From<Month> for u8 exists, so you can just do .into(), or you can do month as u8. Both have the same behavior — January is 1 and December is 12.

sedrik commented 2 years ago

Oh, sorry I totally missed that.

Thanks for the great library :)