Calculate Time Interval/ Difference

To compute time interval, taking into account leap days that are subsumed within that interval.


Updated 15 Jan 2011

View License

main executing reference usage: usage_timeDifference.m

Sample output:
The time interval between earierTime [09-Sep-1900 09:09:18] and laterTime [31-Oct-2010 23:33:08] is
110 years, 79 days, 14 hours, 23 mins, 50.17 secs.

The objective is to compute the time interval taking into account the leap days that are subsumed within the time interval.

The logic subsumes the time interval by
yearEnd, monthEnd, dayEnd - timeEarlier (within that year)
+ time interval (excluding that 2 years)
+ timeLater - yearEnd, monthEnd, dayEnd (within that year)

It differentiates the earlier time and later time, ie. timeDifference (earlierTime, laterTime) gives the same result as timeDifference (laterTime, earlierTime).

leapDayHasPassed.m illustrates a possible approach to determine if a certain day of concern is already over at a given time.

Caveat: does not include leap secs

If the reference demo has a more elegant presentation, please do not hesitate to suggest and send feedback to author.

Thank you.

Michael Chan JT

Cite As

Michael Chan (2023). Calculate Time Interval/ Difference (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Dates and Time in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Included Jan's rendition.

Improve time carry over (to be further refined)
Thank you, Ghee Leng for helping to test.

Retitled from "Calculate Time Interval" to "Calculate Time Interval/ Difference"