day
Day number or name of input date and time
Description
returns the day for each date and time in the input array. By default,
d
= day(t
)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.
Examples
Extract Day Number from Dates
Extract the day of month numbers from an array of dates.
t = [datetime('yesterday');datetime('today');datetime('tomorrow')]
t = 3x1 datetime
04-Sep-2024
05-Sep-2024
06-Sep-2024
d = day(t)
d = 3×1
4
5
6
Determine Day of Week
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
t
— Input date and time
datetime
array
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.
dayType
— Type of day values
'dayofmonth'
(default) | 'dayofweek'
| 'iso-dayofweek'
| 'dayofyear'
| 'name'
| 'shortname'
Type of day values, specified as a value in the table.
Value of
| Description |
---|---|
| Day-of-month number, from 1 to 28, 29, 30, or 31. The range depends on the month. |
| Day-of-week number, from 1 to 7, where day 1 of the week is Sunday. |
| Day-of-week number, from 1 to 7, according to the ISO 8601 standard, where day 1 of the week is Monday. |
| Day-of-year number, from 1 to 365 or 366, depending on the year. |
| Full day names, for example,
|
| Abbreviated day names, for example,
|
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
d
— Day number or name
double
array | cell array of character vectors
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
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
day
function fully supports tall arrays. For more information,
see Tall Arrays.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2014bR2023a: Return ISO day of week
To return the ISO day-of-week number, use the 'iso-dayofweek'
option. In the ISO 8601 standard, Monday is day 1 of the week.
R2022a: Serial date number and text inputs not recommended
Although day
supports serial date number and text inputs,
datetime
values are recommended instead. The
datetime
data type provides flexible date and time formats,
storage out to nanosecond precision, and properties to account for time zones and
daylight saving time.
To convert serial date numbers or text to datetime
values, use
the datetime
function. For
example:
t = datetime(738427.656845093,"ConvertFrom","datenum"); d = day(t)
d = 28
There are no plans to remove support for serial date number or text inputs.
R2022a: day
functions from Financial Toolbox and MATLAB combined
In previous releases, there were two day
functions: one in
MATLAB® that accepted datetime
values, and one in
Financial Toolbox that accepted serial date numbers or text values. As of R2022a, you
can call day
without Financial Toolbox. When you call day
from MATLAB, it accepts all previous input types.
For example, you can make these calls with a serial date number and text from MATLAB:
d = day(738427.656845093)
d = 28
d = day("28-Sep-2021")
d = 28
In addition, day
accepts a second input for the date format
when the first input is not a datetime
array:
d = day(t,F)
In this syntax, specify F
using a valid date format as
documented for the datestr
function, though
'Q'
format specifiers are not supported. For example:
d = day("2021/28/09","yyyy/dd/mm")
d = 28
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)