Main Content

day

Day number or name of input date and time

Description

d = day(t) returns the day for each date and time in the input array. By default, day returns the day-of-month numbers for the elements of the input datetime array t.

The output d 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".

example

Examples

collapse all

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

t = [datetime('yesterday');datetime('today');datetime('tomorrow')]
t = 3x1 datetime
   19-Jul-2024
   20-Jul-2024
   21-Jul-2024

d = day(t)
d = 3×1

    19
    20
    21

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 dayType

Description

'dayofmonth' (default)

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.

'iso-dayofweek' (since R2023a)

Day-of-week number, from 1 to 7, according to the ISO 8601 standard, where day 1 of the week is Monday.

'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