This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Compare Dates and Time

This example shows how to compare datetime and duration arrays. You can perform an element-by-element comparison of values in two datetime arrays or two duration arrays using relational operators, such as > and <.

Compare Datetime Arrays

Compare two datetime arrays. The arrays must be the same size or one can be a scalar.

A = datetime(2013,07,26) + calyears(0:2:6)
A = 1x4 datetime array
   26-Jul-2013   26-Jul-2015   26-Jul-2017   26-Jul-2019

B = datetime(2014,06,01)
B = datetime

A < B
ans = 1x4 logical array

   1   0   0   0

The < operator returns logical 1 (true) where a datetime in A occurs before a datetime in B.

Compare a datetime array to text representing a date.

A >= 'September 26, 2014'
ans = 1x4 logical array

   0   1   1   1

Comparisons of datetime arrays account for the time zone information of each array.

Compare September 1, 2014 at 4:00 p.m. in Los Angeles with 5:00 p.m. on the same day in New York.

A = datetime(2014,09,01,16,0,0,'TimeZone','America/Los_Angeles',...
    'Format','dd-MMM-yyyy HH:mm:ss Z')
A = datetime
   01-Sep-2014 16:00:00 -0700

B = datetime(2014,09,01,17,0,0,'TimeZone','America/New_York',...
    'Format','dd-MMM-yyyy HH:mm:ss Z')
B = datetime
   01-Sep-2014 17:00:00 -0400

A < B
ans = logical

4:00 p.m. in Los Angeles occurs after 5:00 p.m. on the same day in New York.

Compare Durations

Compare two duration arrays.

A = duration([2,30,30;3,15,0])
A = 2x1 duration array

B = duration([2,40,0;2,50,0])
B = 2x1 duration array

A >= B
ans = 2x1 logical array


Compare a duration array to a numeric array. Elements in the numeric array are treated as a number of fixed-length (24-hour) days.

A < [1; 1/24]
ans = 2x1 logical array


Determine if Dates and Time Are Contained Within an Interval

Use the isbetween function to determine whether values in a datetime array lie within a closed interval.

Define endpoints of an interval.

tlower = datetime(2014,08,01)
tlower = datetime

tupper = datetime(2014,09,01)
tupper = datetime

Create a datetime array and determine whether the values lie within the interval bounded by t1 and t2.

A = datetime(2014,08,21) + calweeks(0:2)
A = 1x3 datetime array
   21-Aug-2014   28-Aug-2014   04-Sep-2014

tf = isbetween(A,tlower,tupper)
tf = 1x3 logical array

   1   1   0

See Also

Related Topics