Working with Dates in Rust: Overview of Time and Date Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates in Rust: Overview of Key Libraries



Rust is a systems programming language that places a strong emphasis on safety and performance. However, working with dates and times is always a complex task due to time zones, leap seconds, and formats. The Rust ecosystem has several mature libraries that solve these problems. In this article, we will look at three main libraries: chrono, time, and jiff, and show how to use them in practice.



1. Chrono — The Main Standard

chrono is the most popular library for working with dates in Rust. It provides a full set of tools: parsing, formatting, date arithmetic, time zone handling, and serialization via serde.



Installation via Cargo.toml:

[dependencies]chrono = { version = "0.4", features = ["serde"] }


Basic usage example:

use chrono::{NaiveDate, NaiveDateTime, Duration, Utc, Local};

fn main() { // Current date and time in UTC let utc_now = Utc::now(); println!("Now in UTC: {}", utc_now.format("%Y-%m-%d %H:%M:%S"));

// Local time let local_now = Local::now(); println!("Local time: {}", local_now.format("%d.%m.%Y %H:%M"));

// Creating a specific date let date = NaiveDate::from_ymd_opt(2025, 3, 14).unwrap(); println!("Date: {}", date);

// Date arithmetic let next_week = date + Duration::days(7); println!("In a week: {}", next_week);

// Parsing a string let parsed = NaiveDateTime::parse_from_str( "2025-12-31 23:59:59", "%Y-%m-%d %H:%M:%S" ).unwrap(); println!("Parsed: {}", parsed);}


2. The Time Library — A Modern Alternative

time is a newer and stricter library, developed with the lessons learned from chrono's mistakes. It offers a clear separation between types: Date, Time, OffsetDateTime, and PrimitiveDateTime. It supports macros for convenient date creation.



Installation:

[dependencies]time = { version = "0.3", features = ["macros", "serde"] }


Examples:

use time::{Date, Time, OffsetDateTime, macros::datetime, Duration};

fn main() { // Current time let now = OffsetDateTime::now_utc(); println!("Now: {}", now);

// Creating a date using a macro let meeting = datetime!(2025-06-15 14:30:00 +03:00); println!("Meeting: {}", meeting);

// Extracting components let date = meeting.date(); let time = meeting.time(); println!("Date: {}, Time: {}", date, time);

// Calculating the difference let deadline = datetime!(2025-12-31 00:00:00 UTC); let diff = deadline - now; println!("Until deadline: {} days", diff.whole_days());

// Formatting let formatted = now.format( &time::format_description::well_known::Rfc3339 ).unwrap(); println!("RFC 3339: {}", formatted);}


Important: The time library is part of the official Rust organization and is considered safer as it avoids panics in most methods (uses Result).



3. Jiff — The New Star

jiff is a relatively new library that focuses on ergonomics and performance. Its main feature is automatic time zone detection and built-in calendar support (Gregorian, Islamic, Hebrew).



Installation:

[dependencies]jiff = "0.1"


Usage example:

use jiff::{Zoned, Timestamp, Unit};

fn main() { // Current time with time zone let now = Zoned::now(); println!("Current time: {}", now);

// Creating from a timestamp let ts = Timestamp::new(1_700_000_000, 0

Recommendations