datetime, duration, and calendarDuration arrays
have a Format property that controls the display
of values in each array. When you create a datetime array, it uses
the MATLAB® global default datetime display format unless you
explicitly provide a format. Use dot notation to access the Format property
to view or change its value. For example, to set the display format
for the datetime array, t, to
the default format, type:
t.Format = 'default'Changing the Format property does not change
the values in the array, only their display. For example, the following
can be representations of the same datetime value
(the latter two do not display any time components):
Thursday, August 23, 2012 12:35:00 August 23, 2012 23-Aug-2012
The Format property of the datetime, duration,
and calendarDuration data types accepts different
formats as inputs.
datetime Display FormatYou can set the Format property to one of
these character vectors.
Value of Format | Description |
|---|---|
| Use the default display format. |
| Use the default date display format that does not show time components. |
To change the default formats,
see Default datetime Format.
Alternatively, you can use the letters A-Z and a-z to
specify a custom date format. You can include nonletter characters
such as a hyphen, space, or colon to separate the fields. This table
shows several common display formats and examples of the formatted
output for the date, Saturday, April 19, 2014 at 9:41:06 PM in New
York City.
Value of Format | Example |
|---|---|
'yyyy-MM-dd' | 2014-04-19 |
'dd/MM/yyyy' | 19/04/2014 |
'dd.MM.yyyy' | 19.04.2014 |
'yyyy年 MM月 dd日' | 2014年 04月 19日 |
'MMMM d, yyyy' | April 19, 2014 |
'eeee, MMMM d, yyyy h:mm a' | Saturday, April 19, 2014 9:41 PM |
'MMMM d, yyyy HH:mm:ss Z' | April 19, 2014 21:41:06 -0400 |
'yyyy-MM-dd''T''HH:mmXXX' | 2014-04-19T21:41-04:00 |
For a complete list of valid symbolic identifiers, see the Format property
for datetime arrays.
Note:
The letter identifiers that |
duration Display FormatTo display a duration as a single number that includes a fractional part (for example, 1.234 hours), specify one of these character vectors:
Value of Format | Description |
|---|---|
'y' | Number of exact fixed-length years. A fixed-length year is equal to 365.2425 days. |
'd' | Number of exact fixed-length days. A fixed-length day is equal to 24 hours. |
'h' | Number of hours |
'm' | Number of minutes |
's' | Number of seconds |
To specify the number of fractional digits displayed, user the format function.
To display a duration in the form of a digital timer, specify one of the following character vectors.
'dd:hh:mm:ss'
'hh:mm:ss'
'mm:ss'
'hh:mm'
You also can display up to nine fractional second digits
by appending up to nine S characters. For example, 'hh:mm:ss.SSS' displays
the milliseconds of a duration value to 3 digits.
Changing the Format property does not change
the values in the array, only their display.
calendarDuration Display FormatSpecify the Format property of a calendarDuration array
as a character vector that can include the characters y, q, m, w, d,
and t, in this order. The character vector must
include m to display the number of months, d to
display the number of days, and t to display the
number of hours, minutes, and seconds. The y, q,
and w characters are optional.
This table describes the date and time components that the characters represent.
| Character | Date or Time Unit | Details |
|---|---|---|
y | Years | Multiples of 12 months display as a number of years. |
q | Quarters | Multiples of 3 months display as a number of quarters. |
m | Months | Must be included in Format. |
w | Weeks | Multiples of 7 days display as a number of weeks. |
d | Days | Must be included in Format. |
t | Time (hours, minutes, and seconds) | Must be included in Format. |
To specify the number of digits
displayed for fractional seconds, use the format function.
If the value of a date or time component is zero, it is not displayed.
Changing the Format property does not change
the values in the array, only their display.
datetime FormatYou can set default formats to control the display of datetime arrays
created without an explicit display format. These formats also apply
when you set the Format property of a datetime array
to 'default' or 'defaultdate'.
When you change the default setting, datetime arrays
set to use the default formats are displayed automatically using the
new setting.
Changes to the default formats persist across MATLAB sessions.
To specify a default format, type
datetime.setDefaultFormats('default',fmt)fmt is
a character vector composed of the letters A-Z and a-z described
for the Format property of datetime arrays,
above. For example, datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss')
In addition, you can specify a default format for datetimes created without time components. For example,
datetime.setDefaultFormats('defaultdate','yyyy-MM-dd')
To reset the both the default format and the default date-only formats to the factory defaults, type
datetime.setDefaultFormats('reset')You also can set the default formats in the Preferences dialog box. For more information, see Set Command Window Preferences.
calendarDuration | datetime | datetime Properties | duration | format