How do I get a month name like Jan
or January
from a date?
#392
-
please support |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
A method like this is specifically not available because it is quite sensitive to locale. And, for the most part, Jiff specifically opts out of doing anything with locale. Instead, that's left to crates like With all that said, there are two corners of Jiff that specifically eschew localization. One is its "friendly" duration format. The other is the All of Jiff's datetime types provide convenient access to use jiff::civil;
fn main() -> anyhow::Result<()> {
let zdt =
civil::date(2025, 7, 15).at(17, 30, 0, 0).in_tz("America/New_York")?;
println!("{}", zdt.strftime("%b"));
println!("{}", zdt.strftime("%B"));
Ok(())
} Which has this output:
It might seem like a small step to just add a method to do this for you instead of going through some baroque formatting API to do it, but this is very intentional. It avoids biasing Jiff too much (more than it already does, which is not a zero amount) toward a English-centric view of datetimes. |
Beta Was this translation helpful? Give feedback.
A method like this is specifically not available because it is quite sensitive to locale. And, for the most part, Jiff specifically opts out of doing anything with locale. Instead, that's left to crates like
icu
to handle. Jiff specifically offers integration withicu
viajiff-icu
. The crate docs have examples for how to localize dates. As I understand it (and I could be wrong,icu
's API is vast), there is no simple method to get the month name.With all that said, there are two corners of Jiff that specifically eschew localization. One is its "friendly" duration format. The other is the
jiff::fmt::strtime
APIs. Thestrtime
APIs are meant to provide a simplistic means for ad hoc formattin…