weekday
Day of the week for a date, as a number from 1 (Sunday) to 7 (Saturday).
Category: Language Fundamentals > Data Types
Syntax
n = weekday(t)
Returns the day-of-week number of each date in t.
[n, name] = weekday(t, format)
Also returns the day name; format is 'long' or 'short'.
Description
The numbering starts on Sunday, so Monday is 2 and Saturday is 7. The calculation depends only on the calendar date, not on the time of day.
The result is a plain double array, which makes it handy for indexing a table of weekday labels or for testing weekends with ismember(n, [1 7]).
Input arguments
t- Dates as a
datetimearray, serial date numbers or date text. format- Optional
'long'(for exampleFriday) or'short'(Fri) for the name output.
Output arguments
n- Array of the same size as
twith integers from 1 to 7. name- Day names, one per date, when two outputs are requested.
Example
Three consecutive days
t = datetime(2024, 3, 15) + days(0:2);
weekday(t)
Output
ans = 1×3
6 7 1