This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Set Date and Time Display Format

Formats for Individual Date and Duration Arrays

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

The Format property of the datetime, duration, and calendarDuration data types accepts different formats as inputs.

datetime Display Format

You can set the Format property to one of these character vectors.

Value of FormatDescription


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 FormatExample
'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 datetime accepts are different from those used by the datestr, datenum, and datevec functions.

duration Display Format

To 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 FormatDescription
'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 Format

Specify 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.

CharacterDate or Time UnitDetails
yYearsMultiples of 12 months display as a number of years.
qQuartersMultiples of 3 months display as a number of quarters.
mMonthsMust be included in Format.
wWeeksMultiples of 7 days display as a number of weeks.
dDaysMust be included in Format.
tTime (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.

Default datetime Format

You 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

where 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')
sets the default datetime format to include a 4-digit year, 2-digit month number, 2-digit day number, and hour, minute, and second values.

In addition, you can specify a default format for datetimes created without time components. For example,

sets the default date format to include a 4-digit year, 2-digit month number, and 2-digit day number.

To reset the both the default format and the default date-only formats to the factory defaults, type

The factory default formats depend on your system locale.

You also can set the default formats in the Preferences dialog box. For more information, see Set Command Window Preferences.

See Also

| | | |

Was this topic helpful?