chrono Library for Rust: Complete Guide to Working with Dates and Time
\n\n1. What It Is and Why You Need It
\n\nThe 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.
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\nThe 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.
2. Installation
\n\nTo add chrono to your Rust project, use the Cargo package manager. Run the following in the terminal in the project root directory:
\n\ncargo add chrono\n\nOr manually add the dependency to the Cargo.toml file:
[dependencies]\nchrono = "0.4"\n\nAfter 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:
[dependencies]\nchrono = { version = "0.4", features = ["serde"] }\n\n3. Quick Start — Minimal Working Example
\n\nCreate a file main.rs and paste the following code:
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\nRun 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.
4. Main Methods/Functions
\n\nBelow are 8 key methods and types from the chrono library. For each, the signature, description, and code example are provided.
\n\n4.1. Utc::now()
\nSignature: pub fn now() -> DateTime<Utc>
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.
\nuse 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\n4.2. Local::now()
\nSignature: pub fn now() -> DateTime<Local>
Description: Returns the current moment in time in the local time zone. This method uses the system's time zone settings.
Also in library
Testing in Go (Golang): Frameworks and Tools — Complete Guide
Swagger/OpenAPI Generation for JavaScript (Node.js) — Tools and Libraries
PDF Generation in Java: Overview of iText, Apache PDFBox, OpenPDF Libraries
Working with Dates in Swift (iOS/macOS): Libraries and Tools
ASP.NET Core for C# (.NET): Complete Guide with Examples