Compare Dates and Time
This example shows how to comparedatetime
andduration
arrays. You can perform an element-by-element comparison of values in twodatetime
arrays or twoduration
arrays using relational operators, such as>
and<
.
Compare Datetime Arrays
Compare twodatetime
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 array26-Jul-2013 26-Jul-2015 26-Jul-2017 26-Jul-2019
B = datetime(2014,06,01)
B =datetime01-Jun-2014
A < B
ans =1x4 logical array1 00 0
The<
operator returns logical1
(true) where a datetime inA
occurs before a datetime inB
.
Compare adatetime
array to text representing a date.
A >='September 26, 2014'
ans =1x4 logical array0 1 1 1
Comparisons ofdatetime
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 =datetime01-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 =datetime01-Sep-2014 17:00:00 -0400
A < B
ans =logical0
4:00 p.m. in Los Angeles occurs after 5:00 p.m. on the same day in New York.
Compare Durations
Compare twoduration
arrays.
A = duration([2,30,30;3,15,0])
A =2x1 duration array02:30:30 03:15:00
B = duration([2,40,0;2,50,0])
B =2x1 duration array02:40:00 02:50:00
A >= B
ans =2x1 logical array0 1
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 array1 0
Determine if Dates and Time Are Contained Within an Interval
Use theisbetween
function to determine whether values in adatetime
array lie within a closed interval.
Define endpoints of an interval.
tlower = datetime(2014,08,01)
tlower =datetime01-Aug-2014
tupper = datetime(2014,09,01)
tupper =datetime01-Sep-2014
创建一个datetime
array and determine whether the values lie within the interval bounded byt1
andt2
.
A = datetime(2014,08,21) + calweeks(0:2)
A =1x3 datetime array21-Aug-2014 28-Aug-2014 04-Sep-2014
tf = isbetween(A,tlower,tupper)
tf =1x3 logical array1 1 0