Main Content

day

Day number or name of input date and time

Description

example

d = day(t) returns the day-of-month numbers for each date and time specified in t.

The d output is a double array containing integer values from 1 to 31, depending on the month and year. To assign day values to datetime array t, use t.Day and modify the Day property.

example

d = day(t,dayType) returns the type of day number or name of datetime array t, specified as either "dayofmonth", "dayofweek", "dayofyear", "name", or "shortname".

Examples

collapse all

Extract the day of month numbers from an array of dates.

t = [datetime('yesterday');datetime('today');datetime('tomorrow')]
t = 3x1 datetime
   30-Aug-2022
   31-Aug-2022
   01-Sep-2022

d = day(t)
d = 3×1

    30
    31
     1

Determine the day of the week for an arbitrary date, by specifying 'name' as the second input to the day function.

t = datetime(2014,05,16)
t = datetime
   16-May-2014

d = day(t,'name')
d = 1x1 cell array
    {'Friday'}

Alternatively, specify 'dayofweek' to return the day of the week as a number.

d = day(t,'dayofweek')
d = 6

Input Arguments

collapse all

Input date and time, specified as a datetime array.

To support existing code that previously required Financial Toolbox™, day also accepts serial date numbers and text as inputs, but they are not recommended. For more information, see Version History.

Type of day values, specified as a value in the table.

Value of dayTypeDescription
'dayofmonth'Day-of-month number, from 1 to 28, 29, 30, or 31. The range depends on the month.
'dayofweek'Day-of-week number, from 1 to 7, where day 1 of the week is Sunday.
'dayofyear'Day-of-year number, from 1 to 365 or 366, depending on the year.
'name'Full day names, for example, Sunday. For NaT datetime values, the day name is the empty character vector, ''.
'shortname'Abbreviated day names, for example, Sun. For NaT datetime values, the day name is the empty character vector, ''.

Note

day returns day names in the language specified by the Locale option in the Datetime format section of the Preferences window. To change the default datetime locale, see Set Command Window Preferences.

This argument is valid only when t is a datetime array.

Output Arguments

collapse all

Day number or name, returned as a numeric array of integers of type double, or a cell array of character vectors. d is the same size as t.

Extended Capabilities

Version History

Introduced in R2014b

expand all