Working with Dates in Rust: Overview of Key Libraries
The Rust ecosystem offers several powerful libraries for working with dates and times. Unlike Python with its built-in datetime module, Rust's standard library provides only minimal functionality (std::time::Instant and Duration), so for serious tasks you need to turn to external crates. In this article, we'll look at three main libraries: Chrono, Time, and Jiff.
1. Chrono — The De Facto Standard
Chrono is the most popular library (over 100 million downloads). It provides a full set of tools for working with dates, times, time zones, and formatting.
Key Features:
NaiveDate,NaiveDateTime— dates and times without time zoneDateTime<Tz>— date-time with time zone binding- Support for
Durationand arithmetic operations - Formatting via
format!and string parsing - Calendar support (Gregorian, ISO)
Code Example:
use chrono::{DateTime, Utc, NaiveDate, Duration};
fn main() { // Current time in UTC let now: DateTime<Utc> = Utc::now(); println!("Current time: {}", now.format("%Y-%m-%d %H:%M:%S"));
// Creating a specific date let date = NaiveDate::from_ymd_opt(2025, 3, 15).unwrap(); println!("Date: {}", date);
// Date arithmetic let future = now + Duration::days(30); println!("In 30 days: {}", future.format("%d.%m.%Y"));
// String parsing let parsed = "2025-01-01 12:00:00" .parse::<DateTime<Utc>>() .expect("Invalid date format"); println!("Parsed: {}", parsed);}Chrono is great for most projects, but it can be overkill if you only need basic functionality.
2. Time — A Modern Alternative
The Time crate (by developer Jacob Pratt) positions itself as a safer and more ergonomic replacement for Chrono. It uses strict typing and avoids runtime panics.
Key Differences:
- All operations return
Result— no hidden panics - More strict type system: separate types for dates, times, offsets
- Built-in support for ISO 8601 and RFC 3339
- Smaller size and faster compilation
Code Example:
use time::{Date, Month, OffsetDateTime, Duration, format_description};
fn main() -> Result<(), time::Error> { // Current time let now = OffsetDateTime::now_utc(); println!("Now: {}", now);
// Creating a date (no panic) let date = Date::from_calendar_date(2025, Month::March, 15)?; println!("Date: {}", date);
// Arithmetic let week_later = now + Duration::weeks(1); println!("In a week: {}", week_later);
// Parsing with explicit format let format = format_description::parse("[year]-[month]-[day]")?; let parsed = Date::parse("2025-12-31", &format)?; println!("Parsed: {}", parsed); Ok(())}Time is an excellent choice for new projects where safety and predictability are important.
3. Jiff — The Newest Player
Jiff (by BurntSushi, creator of regex and ripgrep) is the newest library (released in 2024). It focuses on time zone handling and internationalization.
Features:
- Built-in time zone database (IANA)
- Support for non-standard calendars (Islamic, Hebrew, etc.)
- Advanced work with intervals and periods
- High performance through lazy evaluation