datestr

Returns a date as a formatted string.

Syntax

s = datestr(date)

s = datestr(date, fmt)

s = datestr(date, fmt, refYear)

Inputs

date
The date as a number (datenum) or as a string.
Type: double | string
Dimension: scalar | matrix | cell
fmt
The format of the output date string. fmt can be either the index of a predefined format or a string.

The predefined formats are:

Index Format Output
0 'dd-mmm-yyyy HH:MM:SS' 20-Aug-2022 20:40:35
1 'dd-mmm-yyyy' 20-Aug-2022
2 'mm/dd/yy' 08/20/22
3 'mmm' Aug
4 'm' A
5 'mm' 08
6 'mm/dd' 08/20
7 'dd' 20
8 'ddd' Sat
9 'd' S
10 'yyyy' 2022
11 'yy' 22
12 'mmmyy' Aug22
13 'HH:MM:SS' 20:40:35
14 'HH:MM:SS PM' 08:40:35 PM
15 'HH:MM' 20:40
16 'HH:MM PM' 08:40 PM
17 'QQ-YY' Q3-22
18 'QQ' Q3
19 'dd/mm' 20/08
20 'dd/mm/yy' 20/08/22
21 'mmm.dd,yyyy HH:MM:SS' Aug.20,2022 20:40:35
22 'mmm.dd,yyyy' Aug.20,2022
23 'mm/dd/yyyy' 08/20/2022
24 'dd/mm/yyyy' 20/08/2022
25 'yy/mm/dd' 22/08/20
26 'yyyy/mm/dd' 2022/08/20
27 'QQ-YYYY' Q3-2022
28 'mmmyyyy' Aug2022
29 'yyyy-mm-dd' 2022-08-20
30 yyyymmddTHHMMSS 20220820T204035
31 'yyyy-mm-dd HH:MM:SS' 2022-08-20 20:40:35

A format string can be defined using the following string codes:

String code Description Output
'yyyy' 4-digit year 2022
'yy' 2-digit year 22
'mmmm' Full month name August
'mmm' Month name abbreviaton Aug
'mm' Month number 08
'm' First letter of the month A
'dddd' Full day name Saturday
'ddd' Day name abbreviation Sat
'dd' Day of the month 20
'd' First letter of the day name S
'HH' Hour of day 20
'MM' Minutes of hour 40
'SS' Seconds of minute 35
'FFF' Milliseconds of second 120
'AM' or 'PM' 12-hour format 08:40 PM
Type: string
refYear
The year to use as reference for two-digit years. Default value is current year - 50.
Type: integer

Outputs

s
Date string.

Examples

Simple datestr example:

s = datestr(738753)
s = 20-Aug-2022

Use one of the predefined formats to create the date string:

s = datestr(738753:20.2552:738820, 21)
s = 
Aug.20,2022 00:00:00
Sep.09,2022 06:07:29
Sep.29,2022 12:14:58
Oct.19,2022 18:22:27

Set a custom date format:

s1 = datestr(738753:20.2552:738820, 'dd/mmm/yyyy HH:MM:SS.FFF')

s2 = datestr(738753:20.2552:738820, 'dd/mmm/yyyy HH:MM:SS PM')
s1 = 
20/Aug/2022 00:00:00.000
09/Sep/2022 06:07:29.280
29/Sep/2022 12:14:58.560
19/Oct/2022 18:22:27.840

s2 = 
20/Aug/2022 12:00:00 AM
09/Sep/2022 06:07:29 AM
29/Sep/2022 12:14:58 PM
19/Oct/2022 06:22:27 PM

Change the format of a date string:

s = datestr('20-Aug-2022 13:43:16', 2)
s = 08/20/22

Set a reference year for a two-digit year:

s = datestr('08/20/22', 1, 1900)
s = 20-Aug-1922

Cell of strings input:

s = datestr({'01/03/20', '07/15/21', '08/03/22'})
s = 
03-Jan-2020
15-Jul-2021
03-Aug-2022

Use a string fmt with date string input:

s = datestr('20-Aug-2022 13:43:16', 'dd/mmm/yyyy HH:MM:SS.FFF')
s = 20/Aug/2022 13:43:16.000