dayjs

Crates.iodayjs
lib.rsdayjs
version0.2.2
created_at2024-10-24 04:35:23.073898+00
updated_at2025-12-21 15:06:00.223371+00
descriptiona date and time library for Rust, inspired by day.js
homepage
repositoryhttps://github.com/guobin211/dayjs
max_upload_size
id1420854
size119,374
guobin211 (guobin211)

documentation

https://docs.rs/dayjs

README

dayjs

to_string will return the UTC format๏ผŒnot GMT format. use to_gmt for Local Time.

UTC + TIMEZONE = Local Time

Crates.io Documentation License

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 chrono for high performance

Installation

Add this to your Cargo.toml:

[dependencies]
dayjs = "^0.1"

Quick Start

use dayjs::{dayjs, from_str, DisplayTime, OperationTime};

fn main() {
    // Current date and time
    let now = dayjs();
    println!("Now: {}", now);

    // Parse from string
    let mut date = from_str("2025-01-25T10:30:45Z").unwrap();

    // Add 30 days
    date.add_days(30);

    // Format output
    println!("ISO: {}", date.to_iso());
    println!("Custom: {}", date.format("%Y-%m-%d %H:%M:%S"));
}

API Reference

Parse

use dayjs::{from_str, from_int64, from_ymd, from_ymdhms, from_array, from_format};

// From string (supports multiple formats)
let d1 = from_str("2025-01-25T10:30:45Z").unwrap();
let d2 = from_str("2025-01-25 10:30:45").unwrap();
let d3 = from_str("2025/01/25").unwrap();

// From Unix timestamp
let d4 = from_int64(1643164800).unwrap();      // seconds (10 digits)
let d5 = from_int64(1643164800000).unwrap();   // milliseconds (13 digits)

// From components
let d6 = from_ymd(2025, 1, 25).unwrap();
let d7 = from_ymdhms(2025, 1, 25, 10, 30, 45).unwrap();

// From array [year, month(0-11), day, hour, minute, second, ms]
let d8 = from_array(&[2025, 0, 25, 10, 30, 45]).unwrap();

// With custom format
let d9 = from_format("25-01-2025", "%d-%m-%Y").unwrap();

Get + Set

use dayjs::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(2026);
d.set_month(6);     // July (0-indexed)
d.set_date(15);
d.set_hour(12);
d.set_minute(30);
d.set_second(0);

Manipulate

use dayjs::{dayjs, OperationTime};

let mut d = dayjs();

// Add time
d.add_years(1);
d.add_months(2);
d.add_weeks(3);
d.add_days(4);
d.add_hours(5);
d.add_minutes(6);
d.add_seconds(7);
d.add_milliseconds(8);

// Subtract time
d.subtract_years(1);
d.subtract_months(2);
d.subtract_days(3);

// Start of / End of time unit
let start_of_month = d.start_of("month");
let end_of_year = d.end_of("year");

Display

use dayjs::{dayjs, DisplayTime};

let d = dayjs();

// Default to_string (UTC time)
d.to_string();  // "2025-01-25 10:30:45.000 UTC" (UTC format)

// Custom format
d.format("%Y-%m-%d %H:%M:%S");

// 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" (local time)
d.to_array();   // "[ 2025, 0, 25, 10, 30, 45, 0 ]"

Note: The default to_string() method returns UTC time. For local time representation, use to_gmt().

Query

use dayjs::{from_str, QueryTime, Unit};

let d1 = from_str("2025-01-25").unwrap();
let d2 = from_str("2025-01-26").unwrap();
let d3 = from_str("2025-01-27").unwrap();

// Comparison
d1.is_before(&d2);                  // true
d1.is_after(&d2);                   // false
d1.is_same(&d2);                    // false
d1.is_same_unit(&d2, Unit::Month);  // true (same month)

// Inclusive comparison
d1.is_same_or_before(&d2);          // true
d1.is_same_or_after(&d2);           // false

// Range check
d2.is_between(&d1, &d3);            // true

Diff

use dayjs::{from_str, DiffTime, Unit};

let d1 = from_str("2025-01-25").unwrap();
let d2 = from_str("2025-02-25").unwrap();

// Difference by unit
d2.diff(&d1, Unit::Day);    // 31
d2.diff(&d1, Unit::Month);  // 1

// Convenience methods
d2.diff_days(&d1);          // 31
d2.diff_hours(&d1);         // 744
d2.diff_minutes(&d1);       // 44640

Utilities

use dayjs::{dayjs, from_str, min, max};

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("2025-01-01").unwrap();
let d2 = from_str("2025-12-31").unwrap();
let earliest = min(&d1, &d2);
let latest = max(&d1, &d2);

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

Run Tests

# Or manually
pnpm install
pnpm run test

License

MIT

Commit count: 11

cargo fmt