chrono for Rust: Complete Guide with Examples

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

chrono Library for Rust: Complete Guide to Working with Dates and Time

\n\n

1. What It Is and Why You Need It

\n\n

The chrono library is the de facto standard for working with dates and time in the Rust ecosystem. It provides a powerful, type-safe, and ergonomic API for parsing, formatting, arithmetic, and manipulation of timestamps. Unlike the Rust standard library, which offers only basic capabilities through std::time (only for working with durations and system time), chrono covers almost all developer needs: from working with time zones to complex calendar calculations.

\n\n

Why do you need it? If you are writing an application that must process birth dates, log events with nanosecond precision, build schedules, or work with international time zones — you cannot do without chrono. It supports ISO 8601, RFC 2822, RFC 3339 formats, and also allows you to create custom formatters. Thanks to Rust's strict type system, chrono helps avoid errors related to invalid dates (e.g., February 30) or confusion between UTC and local time.

\n\n

The library is actively maintained by the community, has excellent documentation, and is used in thousands of projects, including well-known ones like tokio, actix-web, and diesel. Version 0.4.x is stable and recommended for production use.

\n\n

2. Installation

\n\n

To add chrono to your Rust project, use the Cargo package manager. Run the following in the terminal in the project root directory:

\n\n
cargo add chrono
\n\n

Or manually add the dependency to the Cargo.toml file:

\n\n
[dependencies]\nchrono = "0.4"
\n\n

After that, run cargo build to download the library and its dependencies. To work with time zones (e.g., for parsing city names), you may need to enable the serde or clock feature:

\n\n
[dependencies]\nchrono = { version = "0.4", features = ["serde"] }
\n\n

3. Quick Start — Minimal Working Example

\n\n

Create a file main.rs and paste the following code:

\n\n
use chrono::{Local, Utc, DateTime, Duration};\n\nfn main() {\n    // Current time in UTC\n    let utc_now: DateTime = Utc::now();\n    println!("Current time UTC: {}", utc_now);\n\n    // Current local time\n    let local_now: DateTime = Local::now();\n    println!("Current local time: {}", local_now);\n\n    // Date formatting\n    println!("Formatted date: {}", local_now.format("%Y-%m-%d %H:%M:%S"));\n\n    // Arithmetic: add 2 days\n    let future = utc_now + Duration::days(2);\n    println!("In 2 days (UTC): {}", future);\n}
\n\n

Run the program with the command cargo run. You will see the current time in UTC, local time, a formatted string, and the date in two days.

\n\n

4. Main Methods/Functions

\n\n

Below are 8 key methods and types from the chrono library. For each, the signature, description, and code example are provided.

\n\n

4.1. Utc::now()

\n

Signature: pub fn now() -> DateTime<Utc>

\n

Description: Returns the current moment in time in the UTC time zone. This is the safest way to get a timestamp, as it does not depend on system time zone settings.

\n
use chrono::Utc;\n\nfn main() {\n    let now = Utc::now();\n    println!("Current time in UTC: {}", now);\n    // Output: 2025-03-15 14:30:00.123456 UTC\n}
\n\n

4.2. Local::now()

\n

Signature: pub fn now() -> DateTime<Local>

\n

Description: Returns the current moment in time in the local time zone. This method uses the system's time zone settings.

Recommendations