Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Represent Dates and Times in MATLAB

MATLAB® represents date and time information in any of three formats:

  • Date String — A character string.

    Example:  Thursday, August 23, 2012  9:45:44.946 AM
  • Date Vector — A 1-by-6 numeric vector containing the year, month, day, hour, minute, and second.

    Example:  [2012   8    23    9    45    44.946]
    
  • Serial Date Number — A single number equal to the number of days since January 0, 0000.

    Example:  7.3510e+005

You can use any of these formats. If you work with more than one date and time format, you can convert from one format to another using the datestr, datevec, and datenum functions.

Date Strings

A date string is a character string composed of fields related to a specific date and/or time. There are several ways to represent dates and times in character string format. For example, all of the following are date strings for August 23, 2010 at 04:35:42 PM:

'23-Aug-2010 04:35:06 PM'
'Wednesday, August 23'
'08/23/10 16:35'
'Aug 23 16:35:42.946'

You can represent time in a date string using either a 12-hour or 24-hour system.

When you create a date string, include any characters you might need to separate the fields, such as the hyphen, space, and colon used here:

d = '23-Aug-2010 16:35:42'	

Date Vectors

A date vector is a 1-by-6 matrix of double-precision numbers. Elements of a date vector are integer valued, with the exception of the seconds element, which can be fractional. A date vector is arranged in the following order:

year month day hour minute second

The following date vector represents 10:45:07 AM on October 24, 2012:

[2012  10  24  10  45  07]

Date vectors must follow these guidelines:

  • Date vectors have no separate field in which to specify milliseconds. However, the seconds field has a fractional part and accurately keeps the milliseconds field.

  • Time values are expressed in 24-hour notation. There is no AM or PM setting.

Serial Date Numbers

A serial date number represents a calendar date as the number of days that has passed since a fixed base date. In MATLAB, serial date number 1 is January 1, 0000.

MATLAB also uses serial time to represent fractions of days beginning at midnight; for example, 6 p.m. equals 0.75 serial days. So the string '31-Oct-2003, 6:00 PM' in MATLAB is date number 731885.75.

If you pass date vectors or date strings to a MATLAB function that accepts such inputs, MATLAB first converts the input to serial date numbers. If you are working with a large number of dates or doing extensive calculations with dates, use serial date numbers for better performance.

Was this topic helpful?