Main Content

addtodate

(Not recommended; use duration or calendarDuration) Add time to serial date number

addtodate is not recommended. Use the datetime data type to represent points in time, and duration or calendarDuration values to add time to datetime values. For more information on updating your code, see Version History or Replace Discouraged Instances of Serial Date Numbers and Date Strings.

Description

dt = addtodate(d,t,timeUnit) adds the length of time t to the serial date number d and returns the result as another serial date number dt. The input argument timeUnit specifies the time unit of t. For example, addtodate(d,12,"hours") adds 12 hours to the date and time represented by d.

example

Examples

collapse all

Create a serial date number that represents January 1, 2022.

d = datenum("2022-01-01")
d = 
738522

Add 14 days to d. For display, convert the date number to a datetime value.

dt = addtodate(d,14,"day")
dt = 
738536
datetime(dt,"ConvertFrom","datenum")
ans = datetime
   15-Jan-2022

Subtract 12 hours from dt by specifying the second input argument as a negative number.

dt = addtodate(dt,-12,"hour")
dt = 
7.3854e+05
datetime(dt,"ConvertFrom","datenum")
ans = datetime
   14-Jan-2022 12:00:00

If the added length of time is large enough, then dt rolls over into the appropriate time period, either forward or backward. For example, adding 30 days to dt shifts the date into February.

dt = addtodate(dt,30,"day");
datetime(dt,"ConvertFrom","datenum")
ans = datetime
   13-Feb-2022 12:00:00

Input Arguments

collapse all

Input date and time, specified as a serial date number.

Length of time to add to d, specified as an integer between –1016 and 1016, inclusive.

Time unit of t, specified as one of the listed values.

Version History

Introduced before R2006a

expand all

Go to top of page