datevec

Returns the date vector (year, month, day, hour, minute, seconds) for the given date number or string.

Syntax

v = datevec(date)

v = datevec(date, fmt)

v = datevec(date, refYear)

v = datevec(date, fmt, refYear)

[year, month, day, hour, minute, seconds] = datevec(...)

Inputs

date
The date as a number (datenum) or as a string.
Type: double | string
Dimension: scalar | matrix | cell
fmt
If the date input is a string, fmt is the format to use to read the date. For more information on specifying a date format, see datestr. If an fmt is not provided, the function tries to read the date using the following formats:
  • 'dd mmmm yyyy HH:MM:SS'
  • 'dd mmmm yyyy '
  • 'dd mmm yyyy HH:MM:SS'
  • 'dd mmm yyyy'
  • 'mm/dd/yyyy'
  • 'yyyy/mm/dd'
  • 'mm/dd'
  • 'mmmyy'
  • 'mmmyyyy'
  • 'dd-mmm-yyy HH:MM:SS'
  • 'mmm.dd,yyyy HH:MM:SS'
  • 'mmm.dd,yyyy'
  • 'yyyy-mm-dd HH:MM:SS'
  • 'yyyy-mm-dd'
  • 'HH:MM:SS'
  • 'HH:MM:SS PM'
  • 'HH:MM'
  • 'HH:MM PM'
Type: string
refYear
The year to use as reference for two-digit years. Default value is current year - 50.
Type: integer

Outputs

v
The year, month, day, hour, minute and seconds in a vector.
year, month, day, hour, minute, seconds
The output date components.

Examples

Get the date vector from date numbers:

v1 = datevec(738000)
v2 = datevec(738000:200:739000)
      

v1 = [Matrix] 1 x 6
2020  7  28  0  0  0

v2 = [Matrix] 6 x 6
2020   7  28  0  0  0
2021   2  13  0  0  0
2021   9   1  0  0  0
2022   3  20  0  0  0
2022  10   6  0  0  0
2023   4  24  0  0  0

Get the date vector from a date string:

v = datevec('29-Aug-2022 13:43:16')
v = [Matrix] 1 x 6
2022  8  29  13  43  16

If the date string does not specify a date, then January 1st of the current year is set:

v = datevec({'01:43:16';'01:43:16 PM'})
v = [Matrix] 2 x 6
2022  1  1   1  43  16
2022  1  1  13  43  16

Use fmt to read the date string:

v = datevec('15/05/22 15:32:25.326','dd/mm/yy HH:MM:SS.FFF')
v = [Matrix] 1 x 6
2022.00000  5.00000  15.00000  15.00000  32.00000  25.32600

Use refYear to translate two-digit year to 4-digit year:

v = datevec({'01/03/20', '07/15/21', '08/03/22'}, 1900)
v = [Matrix] 3 x 6
1920  1   3  0  0  0
1921  7  15  0  0  0
1922  8   3  0  0  0