6 releases (breaking)

0.6.0 May 17, 2022
0.5.0 Jan 31, 2022
0.4.0 Nov 2, 2021
0.3.0 Jul 30, 2021
0.1.0 Oct 15, 2020

#5 in Internationalization (i18n)

Download history 55/week @ 2022-03-15 24/week @ 2022-03-22 48/week @ 2022-03-29 39/week @ 2022-04-05 14/week @ 2022-04-12 57/week @ 2022-04-19 178/week @ 2022-04-26 234/week @ 2022-05-03 221/week @ 2022-05-10 1121/week @ 2022-05-17 419/week @ 2022-05-24 660/week @ 2022-05-31 1153/week @ 2022-06-07 1882/week @ 2022-06-14 1733/week @ 2022-06-21 2763/week @ 2022-06-28

7,628 downloads per month
Used in 7 crates (5 directly)

Custom license

26K SLoC

icu_datetime crates.io

icu_datetime is one of the ICU4X components.

This API provides necessary functionality for formatting date and time to user readable textual representation.

[DateTimeFormat] is the main structure of the component. It accepts a set of arguments which allow it to collect necessary data from the data provider, and once instantiated, can be used to quickly format any date and time provided.


use icu::locid::locale;
use icu::calendar::Gregorian;
use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, mock::parse_gregorian_from_str, options::length};

let provider = icu_testdata::get_provider();

// See the next code example for a more ergonomic example with .into().
let options = DateTimeFormatOptions::Length(length::Bag::from_date_time_style(length::Date::Medium, length::Time::Short));

let dtf = DateTimeFormat::<Gregorian>::try_new(locale!("en"), &provider, &options)
    .expect("Failed to create DateTimeFormat instance.");

let date = parse_gregorian_from_str("2020-09-12T12:35:00")
    .expect("Failed to parse date.");

let formatted_date = dtf.format(&date);
assert_eq!(formatted_date.to_string(), "Sep 12, 2020, 12:35 PM");

The options can be created more ergonomically using the Into trait to automatically convert a [options::length::Bag] into a [DateTimeFormatOptions::Length].

use icu::calendar::Gregorian;
use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, options::length};
let options = length::Bag::from_date_time_style(length::Date::Medium, length::Time::Short).into();

let dtf = DateTimeFormat::<Gregorian>::try_new(locale, &provider, &options);

At the moment, the crate provides only options using the Length bag, but in the future, we expect to add more ways to customize the output, like skeletons, and components.

Notice: Rust at the moment does not have a canonical way to represent date and time. We use DateTime as an example of the data necessary for ICU [DateTimeFormat] to work, and we hope to work with the community to develop core date and time APIs that will work as an input for this component. DateTime additionally has support for non-Gregorian calendars, which this module will eventually be able to format.

More Information

For more information on development, authorship, contributing etc. please visit ICU4X home page.