You can create a duration in a single unit using the seconds, minutes, hours, days, or years
functions. For example, create a duration of 2 days, where each day is exactly 24 hours.
d = days(2)
d =
2 days
You can create a calendar duration in a single unit of variable length. For example, one month can be
28, 29, 30, or 31 days long. Specify a calendar duration of 2 months.
L = calmonths(2)
L =
2mo
Use the caldays, calweeks, calquarters, and calyears functions to specify calendar durations
in other units.
Add a number of calendar months and calendar days. The number of days remains separate from the
number of months because the number of days in a month is not fixed, and cannot be determined
until you add the calendar duration to a specific datetime.
L = calmonths(2) + caldays(35)
L =
2mo 35d
Add calendar durations to a datetime to compute a new date.
t2 = t + calmonths(2) + caldays(35)
t2 =
Oct 01, 2014
Oct 02, 2014
t2 is also a datetime array.
whos t2
Name
Size
Bytes Class
Attributes
t2
1x2
161 datetime
In summary, there are several ways to represent dates and times, and MATLAB has a data type for
each approach:
• Represent a point in time, using the datetime data type.
Example: Wednesday, June 18, 2014 10:00:00
• Represent a length of time, or a duration in units of fixed length, using the duration data type.
When using the duration data type, 1 day is always equal to 24 hours, and 1 year is always equal
to 365.2425 days.
Example: 72 hours and 10 minutes
• Represent a length of time, or a duration in units of variable length, using the
calendarDuration data type.
Example: 1 month, which can be 28, 29, 30, or 31 days long.
Represent Dates and Times in MATLAB
7-3
functions. For example, create a duration of 2 days, where each day is exactly 24 hours.
d = days(2)
d =
2 days
You can create a calendar duration in a single unit of variable length. For example, one month can be
28, 29, 30, or 31 days long. Specify a calendar duration of 2 months.
L = calmonths(2)
L =
2mo
Use the caldays, calweeks, calquarters, and calyears functions to specify calendar durations
in other units.
Add a number of calendar months and calendar days. The number of days remains separate from the
number of months because the number of days in a month is not fixed, and cannot be determined
until you add the calendar duration to a specific datetime.
L = calmonths(2) + caldays(35)
L =
2mo 35d
Add calendar durations to a datetime to compute a new date.
t2 = t + calmonths(2) + caldays(35)
t2 =
Oct 01, 2014
Oct 02, 2014
t2 is also a datetime array.
whos t2
Name
Size
Bytes Class
Attributes
t2
1x2
161 datetime
In summary, there are several ways to represent dates and times, and MATLAB has a data type for
each approach:
• Represent a point in time, using the datetime data type.
Example: Wednesday, June 18, 2014 10:00:00
• Represent a length of time, or a duration in units of fixed length, using the duration data type.
When using the duration data type, 1 day is always equal to 24 hours, and 1 year is always equal
to 365.2425 days.
Example: 72 hours and 10 minutes
• Represent a length of time, or a duration in units of variable length, using the
calendarDuration data type.
Example: 1 month, which can be 28, 29, 30, or 31 days long.
Represent Dates and Times in MATLAB
7-3
