dayjs
A Rust library providing a JavaScript Day.js-like API for date and time manipulation, built on top of chrono.
Features
- 🚀 Simple API - JavaScript Day.js-inspired interface for Rust developers
- 🌍 Timezone Support - Handle different timezone formats (offset, city names, numeric)
- 📅 Multiple Parsing Formats - Support for RFC3339, RFC2822, ISO 8601, and more
- ⛓️ Chainable Operations - Add/subtract time units with method chaining
- 🔒 Type Safety - Leverages Rust's type system for safe time operations
- ⚡ Zero-cost Abstractions - Built on
chronofor high performance
Installation
Add this to your Cargo.toml:
[]
= "^0.1"
Quick Start
use ;
API Reference
Parse
use ;
// From string (supports multiple formats)
let d1 = from_str.unwrap;
let d2 = from_str.unwrap;
let d3 = from_str.unwrap;
// From Unix timestamp
let d4 = from_int64.unwrap; // seconds (10 digits)
let d5 = from_int64.unwrap; // milliseconds (13 digits)
// From components
let d6 = from_ymd.unwrap;
let d7 = from_ymdhms.unwrap;
// From array [year, month(0-11), day, hour, minute, second, ms]
let d8 = from_array.unwrap;
// With custom format
let d9 = from_format.unwrap;
Get + Set
use dayjs;
let mut d = dayjs;
// Getters
d.year; // e.g., 2025
d.month; // 0-11 (JavaScript style)
d.date; // 1-31 (day of month)
d.day; // Weekday enum
d.hour; // 0-23
d.minute; // 0-59
d.second; // Unix timestamp in seconds
d.millisecond; // Unix timestamp in milliseconds
// Setters
d.set_year;
d.set_month; // July (0-indexed)
d.set_date;
d.set_hour;
d.set_minute;
d.set_second;
Manipulate
use ;
let mut d = dayjs;
// Add time
d.add_years;
d.add_months;
d.add_weeks;
d.add_days;
d.add_hours;
d.add_minutes;
d.add_seconds;
d.add_milliseconds;
// Subtract time
d.subtract_years;
d.subtract_months;
d.subtract_days;
// Start of / End of time unit
let start_of_month = d.start_of;
let end_of_year = d.end_of;
Display
use ;
let d = dayjs;
// Custom format
d.format;
// Built-in formats
d.to_iso; // "2025-01-25T10:30:45.000Z"
d.to_utc; // "2025-01-25 10:30:45 +00:00"
d.to_gmt; // "Sat, 25 Jan 2025 10:30:45 GMT"
d.to_array; // "[ 2025, 0, 25, 10, 30, 45, 0 ]"
Query
use ;
let d1 = from_str.unwrap;
let d2 = from_str.unwrap;
let d3 = from_str.unwrap;
// Comparison
d1.is_before; // true
d1.is_after; // false
d1.is_same; // false
d1.is_same_unit; // true (same month)
// Inclusive comparison
d1.is_same_or_before; // true
d1.is_same_or_after; // false
// Range check
d2.is_between; // true
Diff
use ;
let d1 = from_str.unwrap;
let d2 = from_str.unwrap;
// Difference by unit
d2.diff; // 31
d2.diff; // 1
// Convenience methods
d2.diff_days; // 31
d2.diff_hours; // 744
d2.diff_minutes; // 44640
Utilities
use ;
let d = dayjs;
// Date utilities
d.days_in_month; // Number of days in current month
d.is_leap_year; // Check if leap year
d.is_valid; // Check if valid date
// Timestamps
d.unix; // Unix timestamp (seconds)
d.value_of; // Milliseconds since epoch
// Clone
let d2 = d.clone_dayjs;
// Min / Max
let d1 = from_str.unwrap;
let d2 = from_str.unwrap;
let earliest = min;
let latest = max;
Supported Parse Formats
| Format | Example |
|---|---|
| RFC 3339 | 2025-01-25T10:30:45Z |
| RFC 2822 | Sat, 25 Jan 2025 10:30:45 +0000 |
| ISO 8601 | 2025-01-25T10:30:45+08:00 |
| Date Time | 2025-01-25 10:30:45 |
| Date Only | 2025-01-25, 2025/01/25 |
| UTC Suffix | 2025-01-25 10:30:45 UTC |