Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

DateTrait

Fully qualified path: chrono::date::DateTrait

pub trait DateTrait

Trait constants

MIN

The minimum possible NaiveDate (January 1, 262144 BCE). (MIN_YEAR << 13) | (1 << 4) | 0o12 /* D */

Fully qualified path: chrono::date::DateTrait::MIN

const MIN: Date;

MAX

The maximum possible NaiveDate (December 31, 262142 CE). (MAX_YEAR << 13) | (365 << 4) | 0o16 /* G */

Fully qualified path: chrono::date::DateTrait::MAX

const MAX: Date;

AFTER_MAX

One day before the minimum possible NaiveDate (December 31, 262145 BCE). One day after the maximum possible NaiveDate (January 1, 262143 CE).

Fully qualified path: chrono::date::DateTrait::AFTER_MAX

const AFTER_MAX: Date;

Trait functions

weeks_from

Fully qualified path: chrono::date::DateTrait::weeks_from

fn weeks_from(self: @Date, day: Weekday) -> i32

from_ordinal_and_flags

Makes a new NaiveDate from year, ordinal and flags. Does not check whether the flags are correct for the provided year.

Fully qualified path: chrono::date::DateTrait::from_ordinal_and_flags

fn from_ordinal_and_flags(year: u32, ordinal: u32, flags: YearFlags) -> Option<Date>

from_mdf

Makes a new NaiveDate from year and packed month-day-flags. Does not check whether the flags are correct for the provided year.

Fully qualified path: chrono::date::DateTrait::from_mdf

fn from_mdf(year: u32, mdf: Mdf) -> Option<Date>

from_ymd_opt

Makes a new NaiveDate from the calendar date (year, month and day).

Errors

Returns None if:

  • The specified calendar day does not exist (for example 2023-04-31).
  • The value for month or day is invalid.
  • year is out of range for NaiveDate.

Example

use chrono::NaiveDate;

let from_ymd_opt = NaiveDate::from_ymd_opt;

assert!(from_ymd_opt(2015, 3, 14).is_some());
assert!(from_ymd_opt(2015, 0, 14).is_none());
assert!(from_ymd_opt(2015, 2, 29).is_none());
assert!(from_ymd_opt(-4, 2, 29).is_some()); // 5 BCE is a leap year
assert!(from_ymd_opt(400000, 1, 1).is_none());
assert!(from_ymd_opt(-400000, 1, 1).is_none());

Fully qualified path: chrono::date::DateTrait::from_ymd_opt

fn from_ymd_opt(year: u32, month: u32, day: u32) -> Option<Date>

from_yo_opt

Makes a new NaiveDate from the ordinal date (year and day of the year).

Errors

Returns None if:

  • The specified ordinal day does not exist (for example 2023-366).
  • The value for ordinal is invalid (for example: 0, 400).
  • year is out of range for NaiveDate.

Example

use chrono::NaiveDate;

let from_yo_opt = NaiveDate::from_yo_opt;

assert!(from_yo_opt(2015, 100).is_some());
assert!(from_yo_opt(2015, 0).is_none());
assert!(from_yo_opt(2015, 365).is_some());
assert!(from_yo_opt(2015, 366).is_none());
assert!(from_yo_opt(-4, 366).is_some()); // 5 BCE is a leap year
assert!(from_yo_opt(400000, 1).is_none());
assert!(from_yo_opt(-400000, 1).is_none());

Fully qualified path: chrono::date::DateTrait::from_yo_opt

fn from_yo_opt(year: u32, ordinal: u32) -> Option<Date>

from_isoywd_opt

Makes a new NaiveDate from the ISO week date (year, week number and day of the week). The resulting NaiveDate may have a different year from the input year.

Errors

Returns None if:

  • The specified week does not exist in that year (for example 2023 week 53).
  • The value for week is invalid (for example: 0, 60).
  • If the resulting date is out of range for NaiveDate.

Example

use chrono::{NaiveDate, Weekday};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let from_isoywd_opt = NaiveDate::from_isoywd_opt;

assert_eq!(from_isoywd_opt(2015, 0, Weekday::Sun), None);
assert_eq!(from_isoywd_opt(2015, 10, Weekday::Sun), Some(from_ymd(2015, 3, 8)));
assert_eq!(from_isoywd_opt(2015, 30, Weekday::Mon), Some(from_ymd(2015, 7, 20)));
assert_eq!(from_isoywd_opt(2015, 60, Weekday::Mon), None);

assert_eq!(from_isoywd_opt(400000, 10, Weekday::Fri), None);
assert_eq!(from_isoywd_opt(-400000, 10, Weekday::Sat), None);

The year number of ISO week date may differ from that of the calendar date.

 use chrono::{NaiveDate, Weekday};
 let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
 let from_isoywd_opt = NaiveDate::from_isoywd_opt;
//           Mo Tu We Th Fr Sa Su
// 2014-W52  22 23 24 25 26 27 28    has 4+ days of new year,
// 2015-W01  29 30 31  1  2  3  4 <- so this is the first week
assert_eq!(from_isoywd_opt(2014, 52, Weekday::Sun), Some(from_ymd(2014, 12, 28)));
assert_eq!(from_isoywd_opt(2014, 53, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2015, 1, Weekday::Mon), Some(from_ymd(2014, 12, 29)));

// 2015-W52  21 22 23 24 25 26 27    has 4+ days of old year,
// 2015-W53  28 29 30 31  1  2  3 <- so this is the last week
// 2016-W01   4  5  6  7  8  9 10
assert_eq!(from_isoywd_opt(2015, 52, Weekday::Sun), Some(from_ymd(2015, 12, 27)));
assert_eq!(from_isoywd_opt(2015, 53, Weekday::Sun), Some(from_ymd(2016, 1, 3)));
assert_eq!(from_isoywd_opt(2015, 54, Weekday::Mon), None);
assert_eq!(from_isoywd_opt(2016, 1, Weekday::Mon), Some(from_ymd(2016, 1, 4)));

Fully qualified path: chrono::date::DateTrait::from_isoywd_opt

fn from_isoywd_opt(year: u32, week: u32, weekday: Weekday) -> Option<Date>

from_num_days_from_ce_opt

Makes a new NaiveDate from a day’s number in the proleptic Gregorian calendar, with January 1, 1 being day 1.

Errors

Returns None if the date is out of range.

Example

use chrono::NaiveDate;

let from_ndays_opt = NaiveDate::from_num_days_from_ce_opt;
let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();

assert_eq!(from_ndays_opt(730_000), Some(from_ymd(1999, 9, 3)));
assert_eq!(from_ndays_opt(1), Some(from_ymd(1, 1, 1)));
assert_eq!(from_ndays_opt(0), Some(from_ymd(0, 12, 31)));
assert_eq!(from_ndays_opt(-1), Some(from_ymd(0, 12, 30)));
assert_eq!(from_ndays_opt(100_000_000), None);
assert_eq!(from_ndays_opt(-100_000_000), None);

Fully qualified path: chrono::date::DateTrait::from_num_days_from_ce_opt

fn from_num_days_from_ce_opt(days: i32) -> Option<Date>

from_weekday_of_month_opt

Makes a new NaiveDate by counting the number of occurrences of a particular day-of-week since the beginning of the given month. For instance, if you want the 2nd Friday of March 2017, you would use NaiveDate::from_weekday_of_month(2017, 3, Weekday::Fri, 2).

n is 1-indexed.

Errors

Returns None if:

  • The specified day does not exist in that month (for example the 5th Monday of Apr. 2023).
  • The value for month or n is invalid.
  • year is out of range for NaiveDate.

Example

use chrono::{NaiveDate, Weekday};
assert_eq!(
    NaiveDate::from_weekday_of_month_opt(2017, 3, Weekday::Fri, 2),
    NaiveDate::from_ymd_opt(2017, 3, 10)
)

Fully qualified path: chrono::date::DateTrait::from_weekday_of_month_opt

fn from_weekday_of_month_opt(year: u32, month: u32, weekday: Weekday, n: u8) -> Option<Date>

checked_add_months

Add a duration in Months to the date

Uses the last day of the month if the day does not exist in the resulting month.

Errors

Returns None if the resulting date would be out of range.

Example

 use chrono::{NaiveDate, Months};
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_months(Months::new(6)),
    Some(NaiveDate::from_ymd_opt(2022, 8, 20).unwrap())
);
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_months(Months::new(2)),
    Some(NaiveDate::from_ymd_opt(2022, 9, 30).unwrap())
);

Fully qualified path: chrono::date::DateTrait::checked_add_months

fn checked_add_months(self: @Date, months: Months) -> Option<Date>

checked_sub_months

Subtract a duration in Months from the date

Uses the last day of the month if the day does not exist in the resulting month.

Errors

Returns None if the resulting date would be out of range.

Example

 use chrono::{NaiveDate, Months};
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_months(Months::new(6)),
    Some(NaiveDate::from_ymd_opt(2021, 8, 20).unwrap())
);

assert_eq!(
    NaiveDate::from_ymd_opt(2014, 1, 1)
        .unwrap()
        .checked_sub_months(Months::new(core::i32::MAX as u32 + 1)),
    None
);

Fully qualified path: chrono::date::DateTrait::checked_sub_months

fn checked_sub_months(self: @Date, months: Months) -> Option<Date>

diff_months

Fully qualified path: chrono::date::DateTrait::diff_months

fn diff_months(self: @Date, months: i32) -> Option<Date>

checked_add_days

Add a duration in Days to the date

Errors

Returns None if the resulting date would be out of range.

Example

 use chrono::{NaiveDate, Days};
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_add_days(Days::new(9)),
    Some(NaiveDate::from_ymd_opt(2022, 3, 1).unwrap())
);
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 7, 31).unwrap().checked_add_days(Days::new(2)),
    Some(NaiveDate::from_ymd_opt(2022, 8, 2).unwrap())
);
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 7,
    31).unwrap().checked_add_days(Days::new(1000000000000)), None
);

Fully qualified path: chrono::date::DateTrait::checked_add_days

fn checked_add_days(self: @Date, days: Days) -> Option<Date>

checked_sub_days

Subtract a duration in Days from the date

Errors

Returns None if the resulting date would be out of range.

Example

 use chrono::{NaiveDate, Days};
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 2, 20).unwrap().checked_sub_days(Days::new(6)),
    Some(NaiveDate::from_ymd_opt(2022, 2, 14).unwrap())
);
assert_eq!(
    NaiveDate::from_ymd_opt(2022, 2,
    20).unwrap().checked_sub_days(Days::new(1000000000000)), None
);

Fully qualified path: chrono::date::DateTrait::checked_sub_days

fn checked_sub_days(self: @Date, days: Days) -> Option<Date>

add_days

Add a duration of i32 days to the date.

Fully qualified path: chrono::date::DateTrait::add_days

fn add_days(self: @Date, days: i32) -> Option<Date>

and_time

Makes a new NaiveDateTime from the current date and given NaiveTime.

Example

use chrono::{NaiveDate, NaiveDateTime, NaiveTime};

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();

let dt: NaiveDateTime = d.and_time(t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);

Fully qualified path: chrono::date::DateTrait::and_time

fn and_time(self: @Date, time: Time) -> DateTime

and_hms_opt

Makes a new NaiveDateTime from the current date, hour, minute and second.

No leap second is allowed here; use NaiveDate::and_hms_*_opt methods with a subsecond parameter instead.

Errors

Returns None on invalid hour, minute and/or second.

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_opt(12, 34, 56).is_some());
assert!(d.and_hms_opt(12, 34, 60).is_none()); // use `and_hms_milli_opt` instead
assert!(d.and_hms_opt(12, 60, 56).is_none());
assert!(d.and_hms_opt(24, 34, 56).is_none());

Fully qualified path: chrono::date::DateTrait::and_hms_opt

fn and_hms_opt(self: @Date, hour: u32, min: u32, sec: u32) -> Option<DateTime>

mdf

Returns the packed month-day-flags.

Fully qualified path: chrono::date::DateTrait::mdf

fn mdf(self: @Date) -> Mdf

with_mdf

Makes a new NaiveDate with the packed month-day-flags changed.

Returns None when the resulting NaiveDate would be invalid.

Fully qualified path: chrono::date::DateTrait::with_mdf

fn with_mdf(self: @Date, mdf: Mdf) -> Option<Date>

succ_opt

Makes a new NaiveDate for the next calendar date.

Errors

Returns None when self is the last representable date.

Example

use chrono::NaiveDate;

assert_eq!(
    NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
    Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap())
);
assert_eq!(NaiveDate::MAX.succ_opt(), None);

Fully qualified path: chrono::date::DateTrait::succ_opt

fn succ_opt(self: @Date) -> Option<Date>

pred_opt

Makes a new NaiveDate for the previous calendar date.

Errors

Returns None when self is the first representable date.

Example

use chrono::NaiveDate;

assert_eq!(
    NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
    Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap())
);
assert_eq!(NaiveDate::MIN.pred_opt(), None);

Fully qualified path: chrono::date::DateTrait::pred_opt

fn pred_opt(self: @Date) -> Option<Date>

checked_add_signed

Adds the number of whole days in the given TimeDelta to the current date.

Errors

Returns None if the resulting date would be out of range.

Example

use chrono::{NaiveDate, TimeDelta};

let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
    d.checked_add_signed(TimeDelta::try_days(40).unwrap()),
    Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(
    d.checked_add_signed(TimeDelta::try_days(-40).unwrap()),
    Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_add_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MAX.checked_add_signed(TimeDelta::try_days(1).unwrap()), None);

Fully qualified path: chrono::date::DateTrait::checked_add_signed

fn checked_add_signed(self: @Date, rhs: TimeDelta) -> Option<Date>

checked_sub_signed

Subtracts the number of whole days in the given TimeDelta from the current date.

Errors

Returns None if the resulting date would be out of range.

Example

use chrono::{NaiveDate, TimeDelta};

let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(
    d.checked_sub_signed(TimeDelta::try_days(40).unwrap()),
    Some(NaiveDate::from_ymd_opt(2015, 7, 27).unwrap())
);
assert_eq!(
    d.checked_sub_signed(TimeDelta::try_days(-40).unwrap()),
    Some(NaiveDate::from_ymd_opt(2015, 10, 15).unwrap())
);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(1_000_000_000).unwrap()), None);
assert_eq!(d.checked_sub_signed(TimeDelta::try_days(-1_000_000_000).unwrap()), None);
assert_eq!(NaiveDate::MIN.checked_sub_signed(TimeDelta::try_days(1).unwrap()), None);

Fully qualified path: chrono::date::DateTrait::checked_sub_signed

fn checked_sub_signed(self: @Date, rhs: TimeDelta) -> Option<Date>

signed_duration_since

Subtracts another NaiveDate from the current date. Returns a TimeDelta of integral numbers.

This does not overflow or underflow at all, as all possible output fits in the range of TimeDelta.

Example

use chrono::{NaiveDate, TimeDelta};

let from_ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
let since = NaiveDate::signed_duration_since;

assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 1)), TimeDelta::zero());
assert_eq!(
    since(from_ymd(2014, 1, 1), from_ymd(2013, 12, 31)),
    TimeDelta::try_days(1).unwrap()
);
assert_eq!(since(from_ymd(2014, 1, 1), from_ymd(2014, 1, 2)),
TimeDelta::try_days(-1).unwrap());
assert_eq!(
    since(from_ymd(2014, 1, 1), from_ymd(2013, 9, 23)),
    TimeDelta::try_days(100).unwrap()
);
assert_eq!(
    since(from_ymd(2014, 1, 1), from_ymd(2013, 1, 1)),
    TimeDelta::try_days(365).unwrap()
);
assert_eq!(
    since(from_ymd(2014, 1, 1), from_ymd(2010, 1, 1)),
    TimeDelta::try_days(365 * 4 + 1).unwrap()
);
assert_eq!(
    since(from_ymd(2014, 1, 1), from_ymd(1614, 1, 1)),
    TimeDelta::try_days(365 * 400 + 97).unwrap()
);

Fully qualified path: chrono::date::DateTrait::signed_duration_since

fn signed_duration_since(self: @Date, rhs: Date) -> TimeDelta

years_since

Returns the number of whole years from the given base until self.

Errors

Returns None if base > self.

Fully qualified path: chrono::date::DateTrait::years_since

fn years_since(self: @Date, base: Date) -> Option<u32>

week

Returns the NaiveWeek that the date belongs to, starting with the Weekday specified.

Fully qualified path: chrono::date::DateTrait::week

fn week(self: @Date, start: Weekday) -> Week

leap_year

Returns true if this is a leap year.

 use chrono::NaiveDate;
assert_eq!(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2001, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2002, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2003, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2004, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2100, 1, 1).unwrap().leap_year(), false);

Fully qualified path: chrono::date::DateTrait::leap_year

fn leap_year(self: @Date) -> bool

year_flags

Fully qualified path: chrono::date::DateTrait::year_flags

fn year_flags(self: @Date) -> YearFlags

from_yof

Create a new NaiveDate from a raw year-ordinal-flags i32.

In a valid value an ordinal is never 0, and neither are the year flags. This method doesn’t do any validation in release builds.

Fully qualified path: chrono::date::DateTrait::from_yof

fn from_yof(yof: u32) -> Date

yof

Get the raw year-ordinal-flags i32.

Fully qualified path: chrono::date::DateTrait::yof

fn yof(self: @Date) -> u32