Enum Weekday

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

Monday.

Tue

Tuesday.

Wed

Wednesday.

Thu

Thursday.

Fri

Friday.

Sat

Saturday.

Sun

Sunday.

Implementations

impl Weekday

const fn succ(self: &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: &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: &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: &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: &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: &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: &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);

impl Clone for Weekday

fn clone(self: &Self) -> Weekday

impl Copy for Weekday

impl Debug for Weekday

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

impl Display for Weekday

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

impl Eq for Weekday

impl Freeze for Weekday

impl FromPrimitive for Weekday

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

impl FromStr for Weekday

fn from_str(s: &str) -> Result<Self, <Self as >::Err>

impl Hash for Weekday

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

impl PartialEq for Weekday

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

impl RefUnwindSafe for Weekday

impl Send for Weekday

impl StructuralPartialEq for Weekday

impl Sync for Weekday

impl TryFrom for Weekday

fn try_from(value: u8) -> Result<Self, <Self as >::Error>

impl Unpin for Weekday

impl UnsafeUnpin for Weekday

impl UnwindSafe for Weekday

impl<T> Any for Weekday

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Weekday

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Weekday

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

impl<T> CloneToUninit for Weekday

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

impl<T> From for Weekday

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Weekday

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for Weekday

fn to_string(self: &Self) -> String

impl<T, U> Into for Weekday

fn into(self: 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 for Weekday

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

impl<T, U> TryInto for Weekday

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