Function Reference
— Function File: str = datestr (date, [f, [p]])

Format the given date/time according to the format f and return the result in str. date is a serial date number (see datenum) or a date vector (see datevec). The value of date may also be a string or cell array of strings.

f can be an integer which corresponds to one of the codes in the table below, or a date format string.

p is the year at the start of the century in which two-digit years are to be interpreted in. If not specified, it defaults to the current year minus 50.

For example, the date 730736.65149 (2000-09-07 15:38:09.0934) would be formatted as follows:

Code Format Example
0 dd-mmm-yyyy HH:MM:SS 07-Sep-2000 15:38:09
1 dd-mmm-yyyy 07-Sep-2000
2 mm/dd/yy 09/07/00
3 mmm Sep
4 m S
5 mm 09
6 mm/dd 09/07
7 dd 07
8 ddd Thu
9 d T
10 yyyy 2000
11 yy 00
12 mmmyy Sep00
13 HH:MM:SS 15:38:09
14 HH:MM:SS PM 03:38:09 PM
15 HH:MM 15:38
16 HH:MM PM 03:38 PM
17 QQ-YY Q3-00
18 QQ Q3
19 dd/mm 13/03
20 dd/mm/yy 13/03/95
21 mmm.dd.yyyy HH:MM:SS Mar.03.1962 13:53:06
22 mmm.dd.yyyy Mar.03.1962
23 mm/dd/yyyy 03/13/1962
24 dd/mm/yyyy 12/03/1962
25 yy/mm/dd 95/03/13
26 yyyy/mm/dd 1995/03/13
27 QQ-YYYY Q4-2132
28 mmmyyyy Mar2047
29 yyyymmdd 20470313
30 yyyymmddTHHMMSS 20470313T132603
31 yyyy-mm-dd HH:MM:SS 1047-03-13 13:26:03

If f is a format string, the following symbols are recognised:

Symbol Meaning Example
yyyy Full year 2005
yy Two-digit year 2005
mmmm Full month name December
mmm Abbreviated month name Dec
mm Numeric month number (padded with zeros) 01, 08, 12
m First letter of month name (capitalized) D
dddd Full weekday name Sunday
ddd Abbreviated weekday name Sun
dd Numeric day of month (padded with zeros) 11
d First letter of weekday name (capitalized) S
HH Hour of day, padded with zeros if PM is set 09:00
and not padded with zeros otherwise 9:00 AM
MM Minute of hour (padded with zeros) 10:05
SS Second of minute (padded with zeros) 10:05:03
PM Use 12-hour time format 11:30 PM

If f is not specified or is -1, then use 0, 1 or 16, depending on whether the date portion or the time portion of date is empty.

If p is nor specified, it defaults to the current year minus 50.

If a matrix or cell array of dates is given, a vector of date strings is returned.

Demonstration 1

The following code

 datestr (now ())

Produces the following output

ans = 27-Oct-2008 21:31:54

Demonstration 2

The following code

 datestr (rem (now (), 1))

Produces the following output

ans = 21:00 AM

Demonstration 3

The following code

 datestr (floor (now ()))

Produces the following output

ans = 27-Oct-2008