Struct chrono::naive::date::NaiveDate [] [src]

pub struct NaiveDate {
    // some fields omitted
}

ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date.

Methods

impl NaiveDate

fn from_ymd(year: i32, month: u32, day: u32) -> NaiveDate

Makes a new NaiveDate from year, month and day. This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.

Panics on the out-of-range date, invalid month and/or day.

Example

use chrono::{NaiveDate, Datelike, Weekday};

let d = NaiveDate::from_ymd(2015, 3, 14);
assert_eq!(d.year(), 2015);
assert_eq!(d.month(), 3);
assert_eq!(d.day(), 14);
assert_eq!(d.ordinal(), 73); // day of year
assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE

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

Makes a new NaiveDate from year, month and day. This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.

Returns None on the out-of-range date, invalid month and/or day.

Example

use chrono::NaiveDate;

let ymd = |y,m,d| NaiveDate::from_ymd_opt(y, m, d);
assert!(ymd(2015, 3, 14).is_some());
assert!(ymd(2015, 0, 14).is_none());
assert!(ymd(2015, 2, 29).is_none());
assert!(ymd(-4, 2, 29).is_some()); // 5 BCE is a leap year
assert!(ymd(400000, 1, 1).is_none());
assert!(ymd(-400000, 1, 1).is_none());

fn from_yo(year: i32, ordinal: u32) -> NaiveDate

Makes a new NaiveDate from year and day of year (DOY or "ordinal"). This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.

Panics on the out-of-range date and/or invalid DOY.

Example

use chrono::{NaiveDate, Datelike, Weekday};

let d = NaiveDate::from_yo(2015, 73);
assert_eq!(d.ordinal(), 73);
assert_eq!(d.year(), 2015);
assert_eq!(d.month(), 3);
assert_eq!(d.day(), 14);
assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday
assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE

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

Makes a new NaiveDate from year and day of year (DOY or "ordinal"). This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE.

Returns None on the out-of-range date and/or invalid DOY.

Example

use chrono::NaiveDate;

let yo = |y,o| NaiveDate::from_yo_opt(y, o);
assert!(yo(2015, 100).is_some());
assert!(yo(2015, 0).is_none());
assert!(yo(2015, 365).is_some());
assert!(yo(2015, 366).is_none());
assert!(yo(-4, 366).is_some()); // 5 BCE is a leap year
assert!(yo(400000, 1).is_none());
assert!(yo(-400000, 1).is_none());

fn from_isoywd(year: i32, week: u32, weekday: Weekday) -> NaiveDate

Makes a new NaiveDate from ISO week date (year and week number) and day of the week (DOW). This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE. The resulting NaiveDate may have a different year from the input year.

Panics on the out-of-range date and/or invalid week number.

Example

use chrono::{NaiveDate, Datelike, Weekday};

let d = NaiveDate::from_isoywd(2015, 11, Weekday::Sat);
assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat));
assert_eq!(d.year(), 2015);
assert_eq!(d.month(), 3);
assert_eq!(d.day(), 14);
assert_eq!(d.ordinal(), 73); // day of year
assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE

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

Makes a new NaiveDate from ISO week date (year and week number) and day of the week (DOW). This assumes the proleptic Gregorian calendar, with the year 0 being 1 BCE. The resulting NaiveDate may have a different year from the input year.

Returns None on the out-of-range date and/or invalid week number.

Example

use chrono::{NaiveDate, Weekday};

let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
let isoywd = |y,w,d| NaiveDate::from_isoywd_opt(y, w, d);

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

// out-of-range dates
assert_eq!(isoywd(400000, 10, Weekday::Fri), None);
assert_eq!(isoywd(-400000, 10, Weekday::Sat), None);

// year boundary behaviors
//
//           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!(isoywd(2014, 52, Weekday::Sun), Some(ymd(2014, 12, 28)));
assert_eq!(isoywd(2014, 53, Weekday::Mon), None);
assert_eq!(isoywd(2015, 1, Weekday::Mon), Some(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!(isoywd(2015, 52, Weekday::Sun), Some(ymd(2015, 12, 27)));
assert_eq!(isoywd(2015, 53, Weekday::Sun), Some(ymd(2016, 1, 3)));
assert_eq!(isoywd(2015, 54, Weekday::Mon), None);
assert_eq!(isoywd(2016, 1, Weekday::Mon), Some(ymd(2016, 1, 4)));

fn from_num_days_from_ce(days: i32) -> NaiveDate

Makes a new NaiveDate from the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar.

Panics on the out-of-range date.

Example

use chrono::{NaiveDate, Datelike, Weekday};

let d = NaiveDate::from_num_days_from_ce(735671);
assert_eq!(d.num_days_from_ce(), 735671); // days since January 1, 1 CE
assert_eq!(d.year(), 2015);
assert_eq!(d.month(), 3);
assert_eq!(d.day(), 14);
assert_eq!(d.ordinal(), 73); // day of year
assert_eq!(d.isoweekdate(), (2015, 11, Weekday::Sat)); // ISO week and weekday

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

Makes a new NaiveDate from the number of days since January 1, 1 (Day 1) in the proleptic Gregorian calendar.

Returns None on the out-of-range date.

Example

use chrono::NaiveDate;

let days = |ndays| NaiveDate::from_num_days_from_ce_opt(ndays);
assert_eq!(days(730000),     Some(NaiveDate::from_ymd(1999, 9, 3)));
assert_eq!(days(1),          Some(NaiveDate::from_ymd(1, 1, 1)));
assert_eq!(days(0),          Some(NaiveDate::from_ymd(0, 12, 31)));
assert_eq!(days(-1),         Some(NaiveDate::from_ymd(0, 12, 30)));
assert_eq!(days(100000000),  None);
assert_eq!(days(-100000000), None);

fn parse_from_str(s: &str, fmt: &str) -> ParseResult<NaiveDate>

Parses a string with the specified format string and returns a new NaiveDate. See the format::strftime module on the supported escape sequences.

Example

use chrono::NaiveDate;

assert_eq!(NaiveDate::parse_from_str("2015-09-05", "%Y-%m-%d"),
           Ok(NaiveDate::from_ymd(2015, 9, 5)));
assert_eq!(NaiveDate::parse_from_str("5sep2015", "%d%b%Y"),
           Ok(NaiveDate::from_ymd(2015, 9, 5)));

Time and offset is ignored for the purpose of parsing.

assert_eq!(NaiveDate::parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
           Ok(NaiveDate::from_ymd(2014, 5, 17)));

Out-of-bound dates or insufficient fields are errors.

assert!(NaiveDate::parse_from_str("2015/9", "%Y/%m").is_err());
assert!(NaiveDate::parse_from_str("2015/9/31", "%Y/%m/%d").is_err());

All parsed fields should be consistent to each other, otherwise it's an error.

assert!(NaiveDate::parse_from_str("Sat, 09 Aug 2013", "%a, %d %b %Y").is_err());

fn and_time(&self, time: NaiveTime) -> NaiveDateTime

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

Example

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

let d = NaiveDate::from_ymd(2015, 6, 3);
let t = NaiveTime::from_hms_milli(12, 34, 56, 789);

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

fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime

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

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

Panics on invalid hour, minute and/or second.

Example

use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};

let d = NaiveDate::from_ymd(2015, 6, 3);

let dt: NaiveDateTime = d.and_hms(12, 34, 56);
assert_eq!(dt.year(), 2015);
assert_eq!(dt.weekday(), Weekday::Wed);
assert_eq!(dt.second(), 56);

fn and_hms_opt(&self, hour: u32, min: u32, sec: u32) -> Option<NaiveDateTime>

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.

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

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 6, 3);
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());

fn and_hms_milli(&self, hour: u32, min: u32, sec: u32, milli: u32) -> NaiveDateTime

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

The millisecond part can exceed 1,000 in order to represent the leap second.

Panics on invalid hour, minute, second and/or millisecond.

Example

use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};

let d = NaiveDate::from_ymd(2015, 6, 3);

let dt: NaiveDateTime = d.and_hms_milli(12, 34, 56, 789);
assert_eq!(dt.year(), 2015);
assert_eq!(dt.weekday(), Weekday::Wed);
assert_eq!(dt.second(), 56);
assert_eq!(dt.nanosecond(), 789_000_000);

fn and_hms_milli_opt(&self, hour: u32, min: u32, sec: u32, milli: u32) -> Option<NaiveDateTime>

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

The millisecond part can exceed 1,000 in order to represent the leap second.

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

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 6, 3);
assert!(d.and_hms_milli_opt(12, 34, 56,   789).is_some());
assert!(d.and_hms_milli_opt(12, 34, 59, 1_789).is_some()); // leap second
assert!(d.and_hms_milli_opt(12, 34, 59, 2_789).is_none());
assert!(d.and_hms_milli_opt(12, 34, 60,   789).is_none());
assert!(d.and_hms_milli_opt(12, 60, 56,   789).is_none());
assert!(d.and_hms_milli_opt(24, 34, 56,   789).is_none());

fn and_hms_micro(&self, hour: u32, min: u32, sec: u32, micro: u32) -> NaiveDateTime

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

The microsecond part can exceed 1,000,000 in order to represent the leap second.

Panics on invalid hour, minute, second and/or microsecond.

Example

use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};

let d = NaiveDate::from_ymd(2015, 6, 3);

let dt: NaiveDateTime = d.and_hms_micro(12, 34, 56, 789_012);
assert_eq!(dt.year(), 2015);
assert_eq!(dt.weekday(), Weekday::Wed);
assert_eq!(dt.second(), 56);
assert_eq!(dt.nanosecond(), 789_012_000);

fn and_hms_micro_opt(&self, hour: u32, min: u32, sec: u32, micro: u32) -> Option<NaiveDateTime>

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

The microsecond part can exceed 1,000,000 in order to represent the leap second.

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

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 6, 3);
assert!(d.and_hms_micro_opt(12, 34, 56,   789_012).is_some());
assert!(d.and_hms_micro_opt(12, 34, 59, 1_789_012).is_some()); // leap second
assert!(d.and_hms_micro_opt(12, 34, 59, 2_789_012).is_none());
assert!(d.and_hms_micro_opt(12, 34, 60,   789_012).is_none());
assert!(d.and_hms_micro_opt(12, 60, 56,   789_012).is_none());
assert!(d.and_hms_micro_opt(24, 34, 56,   789_012).is_none());

fn and_hms_nano(&self, hour: u32, min: u32, sec: u32, nano: u32) -> NaiveDateTime

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

The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.

Panics on invalid hour, minute, second and/or nanosecond.

Example

use chrono::{NaiveDate, NaiveDateTime, Datelike, Timelike, Weekday};

let d = NaiveDate::from_ymd(2015, 6, 3);

let dt: NaiveDateTime = d.and_hms_nano(12, 34, 56, 789_012_345);
assert_eq!(dt.year(), 2015);
assert_eq!(dt.weekday(), Weekday::Wed);
assert_eq!(dt.second(), 56);
assert_eq!(dt.nanosecond(), 789_012_345);

fn and_hms_nano_opt(&self, hour: u32, min: u32, sec: u32, nano: u32) -> Option<NaiveDateTime>

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

The nanosecond part can exceed 1,000,000,000 in order to represent the leap second.

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

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 6, 3);
assert!(d.and_hms_nano_opt(12, 34, 56,   789_012_345).is_some());
assert!(d.and_hms_nano_opt(12, 34, 59, 1_789_012_345).is_some()); // leap second
assert!(d.and_hms_nano_opt(12, 34, 59, 2_789_012_345).is_none());
assert!(d.and_hms_nano_opt(12, 34, 60,   789_012_345).is_none());
assert!(d.and_hms_nano_opt(12, 60, 56,   789_012_345).is_none());
assert!(d.and_hms_nano_opt(24, 34, 56,   789_012_345).is_none());

fn succ(&self) -> NaiveDate

Makes a new NaiveDate for the next date.

Panics when self is the last representable date.

Example

use chrono::NaiveDate;

assert_eq!(NaiveDate::from_ymd(2015,  6,  3).succ(), NaiveDate::from_ymd(2015, 6, 4));
assert_eq!(NaiveDate::from_ymd(2015,  6, 30).succ(), NaiveDate::from_ymd(2015, 7, 1));
assert_eq!(NaiveDate::from_ymd(2015, 12, 31).succ(), NaiveDate::from_ymd(2016, 1, 1));

fn succ_opt(&self) -> Option<NaiveDate>

Makes a new NaiveDate for the next date.

Returns None when self is the last representable date.

Example

use chrono::NaiveDate;
use chrono::naive::date::MAX;

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

fn pred(&self) -> NaiveDate

Makes a new NaiveDate for the prior date.

Panics when self is the first representable date.

Example

use chrono::NaiveDate;

assert_eq!(NaiveDate::from_ymd(2015, 6, 3).pred(), NaiveDate::from_ymd(2015,  6,  2));
assert_eq!(NaiveDate::from_ymd(2015, 6, 1).pred(), NaiveDate::from_ymd(2015,  5, 31));
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).pred(), NaiveDate::from_ymd(2014, 12, 31));

fn pred_opt(&self) -> Option<NaiveDate>

Makes a new NaiveDate for the prior date.

Returns None when self is the first representable date.

Example

use chrono::NaiveDate;
use chrono::naive::date::MIN;

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

fn checked_add(self, rhs: Duration) -> Option<NaiveDate>

Adds the days part of given Duration to the current date.

Returns None when it will result in overflow.

Example

use chrono::{NaiveDate, Duration};
use chrono::naive::date::MAX;

let d = NaiveDate::from_ymd(2015, 9, 5);
assert_eq!(d.checked_add(Duration::days(40)), Some(NaiveDate::from_ymd(2015, 10, 15)));
assert_eq!(d.checked_add(Duration::days(-40)), Some(NaiveDate::from_ymd(2015, 7, 27)));
assert_eq!(d.checked_add(Duration::days(1000_000_000)), None);
assert_eq!(d.checked_add(Duration::days(-1000_000_000)), None);
assert_eq!(MAX.checked_add(Duration::days(1)), None);

fn checked_sub(self, rhs: Duration) -> Option<NaiveDate>

Subtracts the days part of given Duration from the current date.

Returns None when it will result in overflow.

Example

use chrono::{NaiveDate, Duration};
use chrono::naive::date::MIN;

let d = NaiveDate::from_ymd(2015, 9, 5);
assert_eq!(d.checked_sub(Duration::days(40)), Some(NaiveDate::from_ymd(2015, 7, 27)));
assert_eq!(d.checked_sub(Duration::days(-40)), Some(NaiveDate::from_ymd(2015, 10, 15)));
assert_eq!(d.checked_sub(Duration::days(1000_000_000)), None);
assert_eq!(d.checked_sub(Duration::days(-1000_000_000)), None);
assert_eq!(MIN.checked_sub(Duration::days(1)), None);

fn format_with_items<'a, I>(&self, items: I) -> DelayedFormat<I> where I: Iterator<Item=Item<'a>> + Clone

Formats the date with the specified formatting items. Otherwise it is same to the ordinary format method.

The Iterator of items should be Cloneable, since the resulting DelayedFormat value may be formatted multiple times.

Example

use chrono::NaiveDate;
use chrono::format::strftime::StrftimeItems;

let fmt = StrftimeItems::new("%Y-%m-%d");
let d = NaiveDate::from_ymd(2015, 9, 5);
assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05");
assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");

fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>

Formats the date with the specified format string. See the format::strftime module on the supported escape sequences.

This returns a DelayedFormat, which gets converted to a string only when actual formatting happens. You may use the to_string method to get a String, or just feed it into print! and other formatting macros. (In this way it avoids the redundant memory allocation.)

A wrong format string does not issue an error immediately. Rather, converting or formatting the DelayedFormat fails. You are recommended to immediately use DelayedFormat for this reason.

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 9, 5);
assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
assert_eq!(d.format("%A, %-d %B, %C%y").to_string(), "Saturday, 5 September, 2015");

Trait Implementations

impl Datelike for NaiveDate

fn year(&self) -> i32

fn month(&self) -> u32

fn month0(&self) -> u32

fn day(&self) -> u32

fn day0(&self) -> u32

fn ordinal(&self) -> u32

fn ordinal0(&self) -> u32

fn weekday(&self) -> Weekday

fn isoweekdate(&self) -> (i32, u32, Weekday)

fn with_year(&self, year: i32) -> Option<NaiveDate>

fn with_month(&self, month: u32) -> Option<NaiveDate>

fn with_month0(&self, month0: u32) -> Option<NaiveDate>

fn with_day(&self, day: u32) -> Option<NaiveDate>

fn with_day0(&self, day0: u32) -> Option<NaiveDate>

fn with_ordinal(&self, ordinal: u32) -> Option<NaiveDate>

fn with_ordinal0(&self, ordinal0: u32) -> Option<NaiveDate>

fn year_ce(&self) -> (bool, u32)

fn num_days_from_ce(&self) -> i32

impl Hash for NaiveDate

fn hash<H: Hasher>(&self, state: &mut H)

1.3.0fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher

impl Add<Duration> for NaiveDate

An addition of Duration to NaiveDate discards the fractional days, rounding to the closest integral number of days towards Duration::zero().

Panics on underflow or overflow. Use NaiveDate::checked_add for detecting that.

Example

use chrono::{NaiveDate, Duration};

let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
assert_eq!(ymd(2014, 1, 1) + Duration::zero(),             ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) + Duration::seconds(86399),     ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) + Duration::seconds(-86399),    ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) + Duration::days(1),            ymd(2014, 1, 2));
assert_eq!(ymd(2014, 1, 1) + Duration::days(-1),           ymd(2013, 12, 31));
assert_eq!(ymd(2014, 1, 1) + Duration::days(364),          ymd(2014, 12, 31));
assert_eq!(ymd(2014, 1, 1) + Duration::days(365*4 + 1),    ymd(2018, 1, 1));
assert_eq!(ymd(2014, 1, 1) + Duration::days(365*400 + 97), ymd(2414, 1, 1));

type Output = NaiveDate

fn add(self, rhs: Duration) -> NaiveDate

impl Sub<NaiveDate> for NaiveDate

A subtraction of NaiveDate from NaiveDate yields a Duration of integral numbers, and does not overflow or underflow at all.

Example

use chrono::{NaiveDate, Duration};

let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
assert_eq!(ymd(2014, 1, 1) - ymd(2014, 1, 1),   Duration::zero());
assert_eq!(ymd(2014, 1, 1) - ymd(2013, 12, 31), Duration::days(1));
assert_eq!(ymd(2014, 1, 1) - ymd(2014, 1, 2),   Duration::days(-1));
assert_eq!(ymd(2014, 1, 1) - ymd(2013, 9, 23),  Duration::days(100));
assert_eq!(ymd(2014, 1, 1) - ymd(2013, 1, 1),   Duration::days(365));
assert_eq!(ymd(2014, 1, 1) - ymd(2010, 1, 1),   Duration::days(365*4 + 1));
assert_eq!(ymd(2014, 1, 1) - ymd(1614, 1, 1),   Duration::days(365*400 + 97));

type Output = Duration

fn sub(self, rhs: NaiveDate) -> Duration

impl Sub<Duration> for NaiveDate

A subtraction of Duration from NaiveDate discards the fractional days, rounding to the closest integral number of days towards Duration::zero().

Panics on underflow or overflow. Use NaiveDate::checked_sub for detecting that.

Example

use chrono::{NaiveDate, Duration};

let ymd = |y,m,d| NaiveDate::from_ymd(y, m, d);
assert_eq!(ymd(2014, 1, 1) - Duration::zero(),             ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) - Duration::seconds(86399),     ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) - Duration::seconds(-86399),    ymd(2014, 1, 1));
assert_eq!(ymd(2014, 1, 1) - Duration::days(1),            ymd(2013, 12, 31));
assert_eq!(ymd(2014, 1, 1) - Duration::days(-1),           ymd(2014, 1, 2));
assert_eq!(ymd(2014, 1, 1) - Duration::days(364),          ymd(2013, 1, 2));
assert_eq!(ymd(2014, 1, 1) - Duration::days(365*4 + 1),    ymd(2010, 1, 1));
assert_eq!(ymd(2014, 1, 1) - Duration::days(365*400 + 97), ymd(1614, 1, 1));

type Output = NaiveDate

fn sub(self, rhs: Duration) -> NaiveDate

impl Debug for NaiveDate

The Debug output of the naive date d is same to d.format("%Y-%m-%d"). Note that ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.

The string printed can be readily parsed via the parse method on str.

Example

use chrono::NaiveDate;

assert_eq!(format!("{:?}", NaiveDate::from_ymd(2015,  9,  5)), "2015-09-05");
assert_eq!(format!("{:?}", NaiveDate::from_ymd(   0,  1,  1)), "0000-01-01");
assert_eq!(format!("{:?}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31");

// examples of an explicit year sign
assert_eq!(format!("{:?}", NaiveDate::from_ymd(   -1,  1,  1)),  "-0001-01-01");
assert_eq!(format!("{:?}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");

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

impl Display for NaiveDate

The Display output of the naive date d is same to d.format("%Y-%m-%d"). Note that ISO 8601 requires an explicit sign for years before 1 BCE or after 9999 CE.

The string printed can be readily parsed via the parse method on str.

Example

use chrono::NaiveDate;

assert_eq!(format!("{}", NaiveDate::from_ymd(2015,  9,  5)), "2015-09-05");
assert_eq!(format!("{}", NaiveDate::from_ymd(   0,  1,  1)), "0000-01-01");
assert_eq!(format!("{}", NaiveDate::from_ymd(9999, 12, 31)), "9999-12-31");

// examples of an explicit year sign
assert_eq!(format!("{}", NaiveDate::from_ymd(   -1,  1,  1)),  "-0001-01-01");
assert_eq!(format!("{}", NaiveDate::from_ymd(10000, 12, 31)), "+10000-12-31");

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

impl FromStr for NaiveDate

Parsing a str into a NaiveDate uses the same format, %Y-%m-%d, as Debug and Display.

Example

use chrono::NaiveDate;

let d = NaiveDate::from_ymd(2015, 9, 18);
assert_eq!(format!("{}", d).parse::<NaiveDate>(), Ok(d));

let d = NaiveDate::from_ymd(12345, 6, 7);
assert_eq!(format!("{}", d).parse::<NaiveDate>(), Ok(d));

assert!("foo".parse::<NaiveDate>().is_err());

type Err = ParseError

fn from_str(s: &str) -> ParseResult<NaiveDate>

Derived Implementations

impl Clone for NaiveDate

fn clone(&self) -> NaiveDate

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

impl Copy for NaiveDate

impl Ord for NaiveDate

fn cmp(&self, __arg_0: &NaiveDate) -> Ordering

impl PartialOrd for NaiveDate

fn partial_cmp(&self, __arg_0: &NaiveDate) -> Option<Ordering>

fn lt(&self, __arg_0: &NaiveDate) -> bool

fn le(&self, __arg_0: &NaiveDate) -> bool

fn gt(&self, __arg_0: &NaiveDate) -> bool

fn ge(&self, __arg_0: &NaiveDate) -> bool

impl Eq for NaiveDate

impl PartialEq for NaiveDate

fn eq(&self, __arg_0: &NaiveDate) -> bool

fn ne(&self, __arg_0: &NaiveDate) -> bool