etime - (Not recommended; use datetime values or between)
Time elapsed between date vectors - MATLAB ([original](https://in.mathworks.com/help/matlab/ref/etime.html)) ([raw](?raw))
Main Content
(Not recommended; use datetime
values or between
) Time elapsed between date vectors
Syntax
Description
e = etime([t2,t1](#btiguaa-1-t2t1))
returns the number of seconds between two date vectors or matrices of date vectors, t1
andt2
.
Note: To measure the time required to run code, use timeit, or tic and toc, instead ofetime
. For more information, see Tips.
Examples
Compute the time elapsed between a specified time and the current time.
Specify the initial date and time as the start of January 2020. Convert it to date vector form.
t1 = datevec('2020-01-01')
Determine the current date and time by using the datetime
function.
t2 = datetime 01-Feb-2025 08:44:37
Convert the current date and time to a date vector.
t2 = 1×6 103 ×
2.0250 0.0020 0.0010 0.0080 0.0440 0.0370
You can compute the number of seconds between t1
and t2
by using etime
.
However, the etime
function is not recommended. In particular, it is not recommended for measuring the time it takes to run your code.
Input Arguments
Date vectors, specified as 1-by-6 vectors or m
-by-6 matrices containing m
full date vectors in the format:[Year Month Day Hour Minute Second]
.
Example: [2012 03 27 11 50 01]
Data Types: double
Tips
- To time the duration of an event, use the
timeit
ortic
andtoc
functions instead ofetime
andclock
. Theclock
function is based on the system time, which can be adjusted periodically by the operating system, and thus might not be reliable in time comparison operations.
Algorithms
etime
does not account for the following:
- Leap seconds
- Daylight saving time adjustments
- Differences in time zones
Extended Capabilities
Version History
Introduced before R2006a
There are no plans to remove etime
. However, the datetime, duration, and calendarDuration data types are recommended instead. The datetime
data type provides flexible date and time formats, storage out to nanosecond precision, and properties to account for time zones and daylight saving time.
To calculate the elapsed time between two datetime
values, either subtract one from the other or use the between
function. For example, subtract the start of today from the current date and time. Return the elapsed time as a duration
value.
startOfToday = datetime("today") currentTime = datetime("now")
startOfToday = datetime 15-Apr-2022
currentTime = datetime 15-Apr-2022 15:53:28
elapsedTime = currentTime - startOfToday
elapsedTime = duration 15:53:28
To return elapsed time as a calendarDuration
value, usebetween
.
d1 = datetime("2022-01-01") d2 = datetime("now") elapsedTime = between(d1,d2)
elapsedTime = calendarDuration 6mo 18d 13h 19m 18.5561949999974s