[+] Show hidden undocumented itemsReturns the year number in the calendar date.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).year(), 2015);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).year(), -308);
Returns the month number starting from 1.
The return value ranges from 1 to 12.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month(), 9);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month(), 3);
Returns the month number starting from 0.
The return value ranges from 0 to 11.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).month0(), 8);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).month0(), 2);
Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day(), 8);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day(), 14);
Combined with NaiveDate::pred
,
one can determine the number of days in a particular month.
(Note that this panics when year
is out of range.)
use chrono::{NaiveDate, Datelike};
fn ndays_in_month(year: i32, month: u32) -> u32 {
let (y, m) = if month == 12 { (year + 1, 1) } else { (year, month + 1) };
let d = NaiveDate::from_ymd(y, m, 1);
d.pred().day()
}
assert_eq!(ndays_in_month(2015, 8), 31);
assert_eq!(ndays_in_month(2015, 9), 30);
assert_eq!(ndays_in_month(2015, 12), 31);
assert_eq!(ndays_in_month(2016, 2), 29);
assert_eq!(ndays_in_month(2017, 2), 28);
Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).day0(), 7);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).day0(), 13);
Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal(), 251);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal(), 74);
Combined with NaiveDate::pred
,
one can determine the number of days in a particular year.
(Note that this panics when year
is out of range.)
use chrono::{NaiveDate, Datelike};
fn ndays_in_year(year: i32) -> u32 {
let d = NaiveDate::from_ymd(year + 1, 1, 1);
d.pred().ordinal()
}
assert_eq!(ndays_in_year(2015), 365);
assert_eq!(ndays_in_year(2016), 366);
assert_eq!(ndays_in_year(2017), 365);
assert_eq!(ndays_in_year(2000), 366);
assert_eq!(ndays_in_year(2100), 365);
Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).ordinal0(), 250);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).ordinal0(), 73);
Returns the day of week.
use chrono::{NaiveDate, Datelike, Weekday};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).weekday(), Weekday::Tue);
assert_eq!(NaiveDate::from_ymd(-308, 3, 14).weekday(), Weekday::Fri);
Makes a new NaiveDate
with the year number changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(2016),
Some(NaiveDate::from_ymd(2016, 9, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_year(-308),
Some(NaiveDate::from_ymd(-308, 9, 8)));
A leap day (February 29) is a good example that this method can return None
.
assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2015).is_none());
assert!(NaiveDate::from_ymd(2016, 2, 29).with_year(2020).is_some());
Makes a new NaiveDate
with the month number (starting from 1) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(10),
Some(NaiveDate::from_ymd(2015, 10, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month(13), None);
assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month(2), None);
Makes a new NaiveDate
with the month number (starting from 0) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(9),
Some(NaiveDate::from_ymd(2015, 10, 8)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_month0(12), None);
assert_eq!(NaiveDate::from_ymd(2015, 9, 30).with_month0(1), None);
Makes a new NaiveDate
with the day of month (starting from 1) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(30),
Some(NaiveDate::from_ymd(2015, 9, 30)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day(31),
None);
Makes a new NaiveDate
with the day of month (starting from 0) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(29),
Some(NaiveDate::from_ymd(2015, 9, 30)));
assert_eq!(NaiveDate::from_ymd(2015, 9, 8).with_day0(30),
None);
Makes a new NaiveDate
with the day of year (starting from 1) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(60),
Some(NaiveDate::from_ymd(2015, 3, 1)));
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal(366),
None);
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(60),
Some(NaiveDate::from_ymd(2016, 2, 29)));
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal(366),
Some(NaiveDate::from_ymd(2016, 12, 31)));
Makes a new NaiveDate
with the day of year (starting from 0) changed.
Returns None
when the resulting NaiveDate
would be invalid.
use chrono::{NaiveDate, Datelike};
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(59),
Some(NaiveDate::from_ymd(2015, 3, 1)));
assert_eq!(NaiveDate::from_ymd(2015, 1, 1).with_ordinal0(365),
None);
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(59),
Some(NaiveDate::from_ymd(2016, 2, 29)));
assert_eq!(NaiveDate::from_ymd(2016, 1, 1).with_ordinal0(365),
Some(NaiveDate::from_ymd(2016, 12, 31)));
[+] Show hidden undocumented itemsReturns the year number in the calendar date.
See also the NaiveDate::year
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.year(), 2015);
Returns the month number starting from 1.
The return value ranges from 1 to 12.
See also the NaiveDate::month
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.month(), 9);
Returns the month number starting from 0.
The return value ranges from 0 to 11.
See also the NaiveDate::month0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.month0(), 8);
Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
See also the NaiveDate::day
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.day(), 25);
Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
See also the NaiveDate::day0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.day0(), 24);
Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
See also the NaiveDate::ordinal
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.ordinal(), 268);
Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
See also the NaiveDate::ordinal0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.ordinal0(), 267);
Returns the day of week.
See also the NaiveDate::weekday
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike, Weekday};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.weekday(), Weekday::Fri);
Makes a new NaiveDateTime
with the year number changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_year
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 25).and_hms(12, 34, 56);
assert_eq!(dt.with_year(2016), Some(NaiveDate::from_ymd(2016, 9, 25).and_hms(12, 34, 56)));
assert_eq!(dt.with_year(-308), Some(NaiveDate::from_ymd(-308, 9, 25).and_hms(12, 34, 56)));
Makes a new NaiveDateTime
with the month number (starting from 1) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_month
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56);
assert_eq!(dt.with_month(10), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56)));
assert_eq!(dt.with_month(13), None);
assert_eq!(dt.with_month(2), None);
Makes a new NaiveDateTime
with the month number (starting from 0) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_month0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56);
assert_eq!(dt.with_month0(9), Some(NaiveDate::from_ymd(2015, 10, 30).and_hms(12, 34, 56)));
assert_eq!(dt.with_month0(12), None);
assert_eq!(dt.with_month0(1), None);
Makes a new NaiveDateTime
with the day of month (starting from 1) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_day
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_day(30), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56)));
assert_eq!(dt.with_day(31), None);
Makes a new NaiveDateTime
with the day of month (starting from 0) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_day0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_day0(29), Some(NaiveDate::from_ymd(2015, 9, 30).and_hms(12, 34, 56)));
assert_eq!(dt.with_day0(30), None);
Makes a new NaiveDateTime
with the day of year (starting from 1) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_ordinal
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_ordinal(60),
Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56)));
assert_eq!(dt.with_ordinal(366), None);
let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_ordinal(60),
Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56)));
assert_eq!(dt.with_ordinal(366),
Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));
Makes a new NaiveDateTime
with the day of year (starting from 0) changed.
Returns None
when the resulting NaiveDateTime
would be invalid.
See also the
NaiveDate::with_ordinal0
method.
use chrono::{NaiveDate, NaiveDateTime, Datelike};
let dt: NaiveDateTime = NaiveDate::from_ymd(2015, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_ordinal0(59),
Some(NaiveDate::from_ymd(2015, 3, 1).and_hms(12, 34, 56)));
assert_eq!(dt.with_ordinal0(365), None);
let dt: NaiveDateTime = NaiveDate::from_ymd(2016, 9, 8).and_hms(12, 34, 56);
assert_eq!(dt.with_ordinal0(59),
Some(NaiveDate::from_ymd(2016, 2, 29).and_hms(12, 34, 56)));
assert_eq!(dt.with_ordinal0(365),
Some(NaiveDate::from_ymd(2016, 12, 31).and_hms(12, 34, 56)));