Enum Weekday

pub enum Weekday

The day of week.

The order of the days of week depends on the context. (This is why this type does not implement PartialOrd or Ord traits.) One should prefer *_from_monday or *_from_sunday methods to get the correct result.

Example

use chrono::Weekday;

let monday = "Monday".parse::<Weekday>().unwrap();
assert_eq!(monday, Weekday::Mon);

let sunday = Weekday::try_from(6).unwrap();
assert_eq!(sunday, Weekday::Sun);

assert_eq!(sunday.num_days_from_monday(), 6); // starts counting with Monday = 0
assert_eq!(sunday.number_from_monday(), 7); // starts counting with Monday = 1
assert_eq!(sunday.num_days_from_sunday(), 0); // starts counting with Sunday = 0
assert_eq!(sunday.number_from_sunday(), 1); // starts counting with Sunday = 1

assert_eq!(sunday.succ(), monday);
assert_eq!(sunday.pred(), Weekday::Sat);

Variants

Mon = 0

Monday.

Tue = 1

Tuesday.

Wed = 2

Wednesday.

Thu = 3

Thursday.

Fri = 4

Friday.

Sat = 5

Saturday.

Sun = 6

Sunday.

Implementations

impl Weekday

const fn succ(&self) -> Weekday

The next day in the week.

w: Mon Tue Wed Thu Fri Sat Sun
w.succ(): Tue Wed Thu Fri Sat Sun Mon
const fn pred(&self) -> Weekday

The previous day in the week.

w: Mon Tue Wed Thu Fri Sat Sun
w.pred(): Sun Mon Tue Wed Thu Fri Sat
const fn number_from_monday(&self) -> u32

Returns a day-of-week number starting from Monday = 1. (ISO 8601 weekday number)

w: Mon Tue Wed Thu Fri Sat Sun
w.number_from_monday(): 1 2 3 4 5 6 7
const fn number_from_sunday(&self) -> u32

Returns a day-of-week number starting from Sunday = 1.

w: Mon Tue Wed Thu Fri Sat Sun
w.number_from_sunday(): 2 3 4 5 6 7 1
const fn num_days_from_monday(&self) -> u32

Returns a day-of-week number starting from Monday = 0.

w: Mon Tue Wed Thu Fri Sat Sun
w.num_days_from_monday(): 0 1 2 3 4 5 6

Example

# #[cfg(feature = "clock")] {
# use chrono::{Local, Datelike};
// MTWRFSU is occasionally used as a single-letter abbreviation of the weekdays.
// Use `num_days_from_monday` to index into the array.
const MTWRFSU: [char; 7] = ['M', 'T', 'W', 'R', 'F', 'S', 'U'];

let today = Local::now().weekday();
println!("{}", MTWRFSU[today.num_days_from_monday() as usize]);
# }
const fn num_days_from_sunday(&self) -> u32

Returns a day-of-week number starting from Sunday = 0.

w: Mon Tue Wed Thu Fri Sat Sun
w.num_days_from_sunday(): 1 2 3 4 5 6 0
const fn days_since(&self, other: Weekday) -> u32

The number of days since the given day.

Examples

use chrono::Weekday::*;
assert_eq!(Mon.days_since(Mon), 0);
assert_eq!(Sun.days_since(Tue), 5);
assert_eq!(Wed.days_since(Sun), 3);

Trait Implementations

impl Clone for Weekday

fn clone(&self) -> Weekday

impl Copy for Weekday

impl Debug for Weekday

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

impl Display for Weekday

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

impl Eq for Weekday

impl FromPrimitive for Weekday

fn from_i64(n: i64) -> Option<Weekday>
fn from_u64(n: u64) -> Option<Weekday>

impl FromStr for Weekday

type Err = ParseWeekdayError;
fn from_str(s: &str) -> Result<Self, Self::Err>

impl Hash for Weekday

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

impl PartialEq for Weekday

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

impl StructuralPartialEq for Weekday

impl TryFrom<u8> for Weekday

type Error = OutOfRange;
fn try_from(value: u8) -> Result<Self, Self::Error>

Auto Trait Implementations

impl Freeze for Weekday

impl RefUnwindSafe for Weekday

impl Send for Weekday

impl Sync for Weekday

impl Unpin for Weekday

impl UnsafeUnpin for Weekday

impl UnwindSafe for Weekday

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

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

impl<T> From<T> for Weekday

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

fn to_string(&self) -> String

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

fn into(self) -> U

Calls U::from(self).

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

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

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

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

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