Main Content

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
23-Aug-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

'default'

Use the default display format.

'defaultdate'

Use the default date display format that does not show time components.

To change the default formats, see Default datetime Format.

Alternatively, you can specify a custom date format that includes Unicode® characters as literal text. 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. In such formats you can use nonletter ASCII characters such as hyphens, spaces, or colons, or any non-ASCII characters, to separate date and time fields. To include the ASCII letters A-Z and a-z as literal characters in the format, enclose them in quotation marks.

Value of FormatExample
'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 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 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, use 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 format must include m, d, and t.

This table describes the date and time components that the characters represent.

CharacterUnitRequired?
yYears (multiples of 12 months)no
qQuarters (multiples of 3 months)no
mMonthsyes
wWeeks (multiples of 7 days)no
dDaysyes
tTime (hours, minutes, and seconds)yes

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

datetime.setDefaultFormats('default',fmt)
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,

datetime.setDefaultFormats('defaultdate','yyyy-MM-dd')
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

datetime.setDefaultFormats('reset')
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

| | |