Documentation Center

  • Trial Software
  • Product Updates

Compute Elapsed Time

Compute Elapsed Time

Compute the time elapsed between a specific time and the current time, to 0.01-second accuracy.

Define the initial date and time and convert to date vector form.

format shortg
str = 'March 28, 2012 11:51:00';
t1 = datevec(str,'mmmm dd, yyyy HH:MM:SS')
t1 =

        2012           3          28          11          51           0

Determine the current date and time.

t2 = clock
t2 =

         2014            1           17           10           55       54.441

The clock function returns the current date and time as a date vector.

Use etime to compute the number of seconds between t1 and t2.

e = etime(t2,t1)
e =

   5.7021e+07

Compute Future Date

You can compute future dates by adding to a date. This topic shows two ways to do this.

Add Days to a Serial Date Number

This example shows how to add a rational number of days to a serial date number.

The now function returns the current date in serial date number format. You can add a number to this date. For this example, add 50 days.

futuredate = now+50
futuredate =

   7.3567e+05

Alternatively, the initial date might be in date string format. Convert the date to a serial date number using the datenum function.

initialdate = datenum('21.03.2012 13:15','dd.mm.yyyy HH:MM');

Add a rational number of dates to the initial date. In this example, add 5.5 days.

futuredate = initialdate + 5.5
futuredate =

   7.3496e+05

You can convert the future date to date string format.

datestr(futuredate,'dd.mm.yyyy HH:MM')
ans =

27.03.2012 01:15

Add Years, Months, Days, or Time to a Date

This example shows how to add a number of years, months, days, hours, minutes, seconds, or milliseconds to a date in serial date number format. For example, add 5 months to January 3, 2012:

Convert the date string to serial date number format.

initialdate = datenum('03/01/2012','dd/mm/yyyy');

Use the addtodate function to add 5 months to the initial date.

futuredate = addtodate(initialdate,5,'month')
futuredate =

      735023

To convert the future date to a date string, use the datestr function.

datestr(futuredate, 'dddd, mmmm dd')
ans =

Sunday, June 03

See Also

| | |

Was this topic helpful?