Navigation

Operators and Keywords

Function List:

C++ API

: str = datestr (date)
: str = datestr (date, f)
: 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:

CodeFormatExample
0dd-mmm-yyyy HH:MM:SS07-Sep-2000 15:38:09
1dd-mmm-yyyy07-Sep-2000
2mm/dd/yy09/07/00
3mmmSep
4mS
5mm09
6mm/dd09/07
7dd07
8dddThu
9dT
10yyyy2000
11yy00
12mmmyySep00
13HH:MM:SS15:38:09
14HH:MM:SS PM3:38:09 PM
15HH:MM15:38
16HH:MM PM3:38 PM
17QQ-YYQ3-00
18QQQ3
19dd/mm07/09
20dd/mm/yy07/09/00
21mmm.dd,yyyy HH:MM:SSSep.07,2000 15:38:08
22mmm.dd,yyyySep.07,2000
23mm/dd/yyyy09/07/2000
24dd/mm/yyyy07/09/2000
25yy/mm/dd00/09/07
26yyyy/mm/dd2000/09/07
27QQ-YYYYQ3-2000
28mmmyyyySep2000
29yyyy-mm-dd2000-09-07
30yyyymmddTHHMMSS20000907T153808
31yyyy-mm-dd HH:MM:SS2000-09-07 15:38:08

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

SymbolMeaningExample
yyyyFull year2005
yyTwo-digit year05
mmmmFull month nameDecember
mmmAbbreviated month nameDec
mmNumeric month number (padded with zeros)01, 08, 12
mFirst letter of month name (capitalized)D
ddddFull weekday nameSunday
dddAbbreviated weekday nameSun
ddNumeric day of month (padded with zeros)11
dFirst letter of weekday name (capitalized)S
HHHour of day, padded with zeros,09:00
or padded with spaces if PM is set9:00 AM
MMMinute of hour (padded with zeros)10:05
SSSecond of minute (padded with zeros)10:05:03
FFFMilliseconds of second (padded with zeros)10:05:03.012
AMUse 12-hour time format11:30 AM
PMUse 12-hour time format11: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 column vector of date strings is returned.

See also: datenum, datevec, date, now, clock.

Demonstration 1

The following code

 ## Current date and time in default format
 datestr (now ())

Produces the following output

ans = 02-Jan-2017 11:55:22

Demonstration 2

The following code

 ## Current date (integer portion of datenum)
 datestr (fix (now ()))

Produces the following output

ans = 02-Jan-2017

Demonstration 3

The following code

 ## Current time (fractional portion of datenum)
 datestr (rem (now (), 1))

Produces the following output

ans = 11:55 AM

Package: octave