datestr

Convert date and time to string format.

Syntax

  • dateAsString = datestr(dateVector)

  • dateAsString = datestr(dateNumber)

  • dateAsString = datestr(..., formatOut)

  • dateAsString = datestr(dateAsStringIn)

  • dateAsString = datestr(dateAsStringIn, formatOut, pivotYear)

  • dateAsString = datestr(..., 'local')

Input argument

  • dateVector - Date vectors or matrix.

  • dateNumber - Serial date numbers: An array of positive double-precision floating-point numbers.

  • formatOut - character vector, string scalar or integer value (-1 default): Output format for representing dates and times

  • dateAsStringIn - character vector, cell array or string array: text denoting dates and times to convert.

  • pivotYear - integer value: present minus 50 years (default).

  • 'local' - returns the date in the language of the current locale.

Output argument

  • dateAsString - character vector or two-dimensional character array: text denoting dates and time.

Description

dateAsString = datestr(dateVector) converts date vectors into text that represents the corresponding dates and times. It returns a character array with m rows, where m is the number of date vectors in dateVector.

dateAsString = datestr(dateNumber) converts serial date numbers into text representing dates and times. The output is a character array with m rows, where m is the number of date numbers in dateNumber.

dateAsString = datestr(..., formatOut) allows you to specify the format of the output text using formatOut. You can apply this option with any of the previous input types.

dateAsString = datestr(dateAsStringIn) converts the input string dateAsStringIn into a text format of day-month-year hour:minute:second. All dates in dateAsStringIn must follow the same format.

dateAsString = datestr(dateAsStringIn, formatOut, pivotYear) converts dateAsStringIn into the format specified by formatOut, while using an optional pivotYear to interpret two-digit years.

dateAsString = datestr(..., 'local') returns the date in the language of the system's current locale. If 'local' is omitted, the default language is US English. The 'local' option can be used with any of the previous syntaxes, and must be the last argument in the sequence.

Supported format conversion:

dd-mmm-yyyy HH:MM:SS 10-Mar-2010 16:48:17

dd-mmm-yyyy 10-Mar-2010

mm/dd/yyyy 03/10/2010

mm/dd/yy 03/10/00

mm/dd 03/10

mmm.dd,yyyy HH:MM:SS Mar.10,2010 16:48:17

mmm.dd,yyyy Mar.10,2010

yyyy-mm-dd HH:MM:SS 2010-03-10 16:48:17

yyyy-mm-dd 2010-03-10

yyyy/mm/dd 2000/03/10

HH:MM:SS 16:48:17

HH:MM:SS PM 3:48:17 PM

HH:MM 16:48

HH:MM PM 3:35 PM

If format is not specified, the default format is dd-mmm-yyyy.

If format is specified and not using predefined format, the format must be specified as a character vector or string scalar composed of symbolic identifiers.

The format of the input text for representing dates and times, expressed as a character vector or string scalar composed of symbolic identifiers.

Examples

dateVector = [2019, 4, 2, 9, 7, 18];
datestr(dateVector)
dateVector = [2019, 4, 2, 9, 7, 18];
formatOut = 'mm/dd/yy';
datestr(dateVector, formatOut)
datestr(now, 'mmmm dd, yyyy HH:MM:SS.FFF AM')
datestr('06:33 PM','HH:MM')
datestr('06:33','HH:MM PM')
formatOut = 'dd mmm yyyy';
datestr(datenum('18-05-45','dd-mm-yy',1900),formatOut)
datestr(datenum({'09/17/2017';'06/14/1906';'10/29/2014'}, 'mm/dd/yyyy')))
dateStringIn = '5/17/56';
formatOut = 1;
pivotYear = 1900;
datestr(dateStringIn, formatOut, pivotYear)
pivotYear = 2000;
datestr(dateStringIn,formatOut, pivotYear)

See also

datenum, datevec.

History

Author

Allan CORNET

Last updated