Dayjs

Struct Dayjs 

Source
pub struct Dayjs { /* private fields */ }
Expand description

Dayjs struct representing a date and time with a time zone.

It contains:

  • tz: The time zone information as a TimeZone enum.
  • time: The UTC time as a DateTime<Utc>.

§Examples

use dayjs::{dayjs, Dayjs, TimeZone};
let now = dayjs();

Implementations§

Source§

impl Dayjs

Source

pub fn to_object(&self) -> Value

Convert Dayjs to JSON object

§Returns

Returns a JSON object with timezone and time information

§Examples
use dayjs::dayjs;
let d = dayjs();
let obj = d.to_object();
println!("{}", obj);
Source

pub fn from_object(obj: Value) -> Result<Self, String>

Create Dayjs from JSON object

§Parameters
  • obj: JSON object containing “tz” and “time” fields
§Returns

Returns a Result with Dayjs on success, error message on failure

§Examples
use dayjs::Dayjs;
use serde_json::json;

let obj = json!({
    "tz": "+08:00",
    "time": "2025-12-21T14:30:00Z"
});
let d = Dayjs::from_object(obj).unwrap();
Source§

impl Dayjs

Source

pub fn format(&self, template: &str) -> String

Format the date time according to the given template.

§Parameters
  • template: %Y-%m-%d %H:%M:%S
§Examples
let now = dayjs::dayjs();
let formatted = now.format("%Y-%m-%d %H:%M:%S");
println!("{}", formatted);
// 2025-03-25 17:21:47
Source

pub fn set_timezone(&mut self, tz: TimeZone)

Set the time zone for the Dayjs instance.

Source

pub fn get_timezone(&self) -> &TimeZone

Get the current time zone of the Dayjs instance.

Source

pub fn get_timestamp(&self) -> i64

Get the current time in UTC.

Source

pub fn timestamp(&self) -> i64

Get the current time in second.

Source

pub fn millisecond(&self) -> u32

Get the millisecond (0-999)

Source

pub fn second(&self) -> u32

Get the second (0-59)

Source

pub fn minute(&self) -> u32

Get the minute (0-59)

Source

pub fn hour(&self) -> u32

Get the hour (0-23)

Source

pub fn date(&self) -> u32

Get the date of month 1 ~ 31

Source

pub fn day(&self) -> Weekday

Get the week number 1 ~ 7

Source

pub fn day_of_year(&self) -> u32

Get the month number 1 ~ 366

Source

pub fn week_of_year(&self) -> u32

Get the week number 1 ~ 53

Source

pub fn month_of_year(&self) -> u32

Get the month number 1 ~ 12

Source

pub fn year(&self) -> i32

Get the year

Source

pub fn month(&self) -> u32

Get the month (0-11, JavaScript style)

Source

pub fn set_year(&mut self, year: i32)

Set the year

Source

pub fn set_month(&mut self, month: u32)

Set the month (1-12)

Source

pub fn set_date(&mut self, day: u32)

Set the day of month (1-31)

Source

pub fn set_hour(&mut self, hour: u32)

Set the hour (0-23)

Source

pub fn set_minute(&mut self, minute: u32)

Set the minute (0-59)

Source

pub fn set_second(&mut self, second: u32)

Set the second (0-59)

Source

pub fn set_millisecond(&mut self, ms: u32)

Set the millisecond (0-999)

Source

pub fn start_of(&self, unit: &str) -> Dayjs

Get start of a unit of time

Source

pub fn end_of(&self, unit: &str) -> Dayjs

Get end of a unit of time

Source

pub fn clone_dayjs(&self) -> Dayjs

Clone the Dayjs instance

Source

pub fn days_in_month(&self) -> u32

Get the number of days in the month

Source

pub fn is_leap_year(&self) -> bool

Check if the year is a leap year

Source

pub fn value_of(&self) -> i64

Convert to JavaScript Date valueOf (milliseconds since epoch)

Source

pub fn unix(&self) -> i64

Convert to Unix timestamp (seconds since epoch)

Source

pub fn is_valid(&self) -> bool

Check if the Dayjs object is valid

Trait Implementations§

Source§

impl Clone for Dayjs

Source§

fn clone(&self) -> Dayjs

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dayjs

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Dayjs

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl DiffTime for Dayjs

Source§

fn diff(&self, other: &Dayjs, unit: Unit) -> i64

Get the difference between two times in the specified unit
Source§

fn diff_milliseconds(&self, other: &Dayjs) -> i64

Get the difference in milliseconds
Source§

fn diff_seconds(&self, other: &Dayjs) -> i64

Get the difference in seconds
Source§

fn diff_minutes(&self, other: &Dayjs) -> i64

Get the difference in minutes
Source§

fn diff_hours(&self, other: &Dayjs) -> i64

Get the difference in hours
Source§

fn diff_days(&self, other: &Dayjs) -> i64

Get the difference in days
Source§

fn diff_weeks(&self, other: &Dayjs) -> i64

Get the difference in weeks
Source§

fn diff_months(&self, other: &Dayjs) -> i64

Get the difference in months
Source§

fn diff_years(&self, other: &Dayjs) -> i64

Get the difference in years
Source§

impl Display for Dayjs

Display implementation for Dayjs Displays the UTC time in ISO 8601 format (e.g., 2025-12-21T14:29:42.009427Z)

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl DisplayTime for Dayjs

Source§

fn to_array(&self) -> String

Formats the date time to an array string.

Source§

fn to_iso(&self) -> String

Formats the date time to an ISO 8601 string.

Source§

fn to_utc(&self) -> String

Formats the date time to a UTC string.

Source§

fn to_gmt(&self) -> String

Formats the date time to a GMT string.

Source§

fn to_timestamp(&self) -> i64

Converts the time to a timestamp in seconds.

Source§

fn to_local(&self) -> String

Formats the date time to a local string.

Source§

impl OperationTime for Dayjs

Source§

fn add(&mut self, timestamp: i32)

Add a duration to the current time.

Source§

fn add_years(&mut self, years: i32)

Add a duration to the current time in year.

Source§

fn add_months(&mut self, months: i32)

Add a duration to the current time in month.

Source§

fn add_weeks(&mut self, weeks: i32)

Add a duration to the current time in week.

Source§

fn add_days(&mut self, days: i32)

Add a duration to the current time in day.

Source§

fn add_hours(&mut self, hours: i32)

Add a duration to the current time in hours.

Source§

fn add_minutes(&mut self, minutes: i32)

Add a duration to the current time in minutes.

Source§

fn add_seconds(&mut self, seconds: i32)

Add a duration to the current time in seconds.

Source§

fn add_milliseconds(&mut self, milliseconds: i32)

Add a duration to the current time in milliseconds.

Source§

fn subtract(&mut self, timestamp: i32)

Subtract a duration from the current time.

Source§

fn subtract_years(&mut self, years: i32)

Subtract a duration from the current time in year.

Source§

fn subtract_months(&mut self, months: i32)

Subtract a duration from the current time in month.

Source§

fn subtract_weeks(&mut self, weeks: i32)

Subtract a duration from the current time in week.

Source§

fn subtract_days(&mut self, days: i32)

Subtract a duration from the current time in day.

Source§

fn subtract_hours(&mut self, hours: i32)

Subtract a duration from the current time in hours.

Source§

fn subtract_minutes(&mut self, minutes: i32)

Subtract a duration from the current time in minutes.

Source§

fn subtract_seconds(&mut self, seconds: i32)

Subtract a duration from the current time in seconds.

Source§

fn subtract_milliseconds(&mut self, milliseconds: i32)

Subtract a duration from the current time in milliseconds.

Source§

impl PartialEq for Dayjs

Source§

fn eq(&self, other: &Dayjs) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl QueryTime for Dayjs

Source§

fn is_before(&self, other: &Dayjs) -> bool

Check if this time is before another time
Source§

fn is_before_unit(&self, other: &Dayjs, unit: Unit) -> bool

Check if this time is before another time with unit granularity
Source§

fn is_after(&self, other: &Dayjs) -> bool

Check if this time is after another time
Source§

fn is_after_unit(&self, other: &Dayjs, unit: Unit) -> bool

Check if this time is after another time with unit granularity
Source§

fn is_same(&self, other: &Dayjs) -> bool

Check if this time is the same as another time
Source§

fn is_same_unit(&self, other: &Dayjs, unit: Unit) -> bool

Check if this time is the same as another time with unit granularity
Source§

fn is_same_or_before(&self, other: &Dayjs) -> bool

Check if this time is the same or before another time
Source§

fn is_same_or_after(&self, other: &Dayjs) -> bool

Check if this time is the same or after another time
Source§

fn is_between(&self, start: &Dayjs, end: &Dayjs) -> bool

Check if this time is between two other times
Source§

fn is_between_unit(&self, start: &Dayjs, end: &Dayjs, unit: Unit) -> bool

Check if this time is between two other times with unit granularity
Source§

impl StructuralPartialEq for Dayjs

Auto Trait Implementations§

§

impl Freeze for Dayjs

§

impl RefUnwindSafe for Dayjs

§

impl Send for Dayjs

§

impl Sync for Dayjs

§

impl Unpin for Dayjs

§

impl UnwindSafe for Dayjs

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.