Compare Dates and Times Using Relational Operators
Relational operators compare arrays quantitatively, using operators such as "less than," "greater than," and "equal to." The result of a relational comparison is a logical array indicating the locations where the relation is true.
The datetime
and duration
data types represent
dates and times quantitatively. To compare such arrays, you can use the same relational
operators that you use to compare numeric arrays. However, the comparisons have meanings
that are slightly different, depending on the data type.
Relational Operator | Meaning When Comparing | Meaning When Comparing |
---|---|---|
| Before | A shorter length of time than |
| Either before or at the same date and time as | Either a shorter length of time than or the same length of time as |
| After | A longer length of time than |
| Either after or at the same date and time as | Either a longer length of time than or the same length of time as |
| At the same date and time as | The same length of time as |
| Not at the same date and time as | Not the same length of time as |
Note: The calendarDuration
data type
does not support comparisons using relational operators. Calendar years, quarters, months,
weeks, and days do not necessarily represent fixed lengths of time, thanks to leap years,
months of different lengths, daylight saving time, and leap seconds.
Compare Dates and Times
You can compare two datetime
arrays to each other, and you can compare two duration
arrays to each other. The arrays must have compatible sizes because relational operators perform elementwise comparisons. In the simplest cases, the two arrays have the same size or one is a scalar. In more complex cases, MATLAB® implicitly expands arrays with compatible sizes to be the same size during the comparison. For more information, see Compatible Array Sizes for Basic Operations.
Also, you can compare datetime
and duration
arrays to:
Text. When you compare text to a
datetime
orduration
array, MATLAB implicitly converts the text to thedatetime
orduration
value that it represents.Numbers. When you compare a number to a
duration
array, MATLAB implicitly converts that number to aduration
value that is the equivalent number of days.
You cannot compare a datetime
array and a duration
array. However, you can compare components of datetime
arrays to numbers or to duration
arrays.
Compare datetime
Arrays
Create a datetime
array. To convert text representing a date and time, use the datetime
function.
D1 = datetime("2022-06-05 11:37:05")
D1 = datetime
05-Jun-2022 11:37:05
You can also convert numbers to datetime
arrays. The input numeric arrays represent datetime
components—years, months, days, hours, minutes, and seconds.
D2 = datetime(2022,2:4:10,15,12,0,0)
D2 = 1x3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00
Compare the two datetime
arrays. The result shows which elements of D2
occur after D1
.
TF = D1 < D2
TF = 1x3 logical array
0 1 1
To create a new datetime
array containing only the matching elements, index into D2
using TF
.
afterD1 = D2(TF)
afterD1 = 1x2 datetime
15-Jun-2022 12:00:00 15-Oct-2022 12:00:00
Text and datetime
Arrays
If you have text that represents dates and times, and it is formatted in a way that the datetime
function recognizes, then you can compare the text to a datetime
array. The comparison implicitly converts the text.
For example, compare a string that represents June 1, 2022 to D2
. The first element of D2
occurs before June 1. (If you specify a date only, then the time is set to midnight.)
TF = D2 >= "2022-06-01"
TF = 1x3 logical array
0 1 1
afterJune1 = D2(TF)
afterJune1 = 1x2 datetime
15-Jun-2022 12:00:00 15-Oct-2022 12:00:00
Components of datetime
Arrays
The datetime
data type provides access to the components of datetime
values. Access components by using the year
, quarter
, month
, day
, hour
, minute
, and second
functions. You can compare components to numbers or duration
values because these functions return numbers.
For example, display the datetime
array D2
. Then display its month component.
D2
D2 = 1x3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00
M = month(D2)
M = 1×3
2 6 10
To find the elements of D2
that occur before the month of June, compare D2
to the numeric value corresponding to June. Then index into D2
.
TF = month(D2) < 6
TF = 1x3 logical array
1 0 0
beforeJune = D2(TF)
beforeJune = datetime
15-Feb-2022 12:00:00
Compare duration
Arrays
Create a duration
array. One way to create it is to use the duration
function to convert text that represents a length of time.
For example, convert text in hh:mm:ss
format.
T1 = duration("03:37:12")
T1 = duration
03:37:12
You can also convert numbers to duration
arrays. The input numeric arrays represent hours, minutes, and seconds.
T2 = duration(0:2:6,30,0)
T2 = 1x4 duration
00:30:00 02:30:00 04:30:00 06:30:00
Compare the two duration
arrays. The result show which elements of T2
are longer than T1
.
TF = T1 < T2
TF = 1x4 logical array
0 0 1 1
To create a new duration
array containing only the matching elements, index into T2
using TF
.
longerThanT1 = T2(TF)
longerThanT1 = 1x2 duration
04:30:00 06:30:00
Text and duration
Arrays
If you have text that represents a length of time, and it is formatted in a way that the duration
function recognizes, then you can compare the text to a duration
array. The comparison implicitly converts the text.
For example, compare a string that represents two hours and five minutes to T2
. The first element of T2
is shorter.
T2 >= "02:05:00"
ans = 1x4 logical array
0 1 1 1
longerThan205 = T2(TF)
longerThan205 = 1x2 duration
04:30:00 06:30:00
Numbers and duration
Arrays
You can compare numeric arrays to duration
arrays. The comparison treats a numeric value as a number of fixed-length (24-hour) days.
Compare the elements of T2
to one day. Every element is shorter.
TF = T2 < 1
TF = 1x4 logical array
1 1 1 1
T2(TF)
ans = 1x4 duration
00:30:00 02:30:00 04:30:00 06:30:00
Compare the elements of T2
to one hour. Only the first element of T2
is shorter.
TF = T2 < 1/24
TF = 1x4 logical array
1 0 0 0
T2(TF)
ans = duration
00:30:00
Comparisons Across Different Time Zones
Create datetime
values for October 1, 2022, at 4:00 p.m. in Los Angeles and October 1, 2022 at 5:00 p.m. in New York. The two cities are so far apart that they are in different time zones.
You can create datetime
arrays with time zones by specifying the TimeZone
name-value argument. To show the time zone when displaying these values, specify the Format
name-value argument. Note that you can specify a datetime
display format that differs from the format of the input text.
LAtime = datetime("2022-10-01 16:00:00", ... "TimeZone","America/Los_Angeles",... "Format","dd-MMM-yyyy hh:mm:ss a z")
LAtime = datetime
01-Oct-2022 04:00:00 PM PDT
NYtime = datetime("2022-10-01 17:00:00", ... "TimeZone","America/New_York",... "Format","dd-MMM-yyyy hh:mm:ss a z")
NYtime = datetime
01-Oct-2022 05:00:00 PM EDT
Compare the times in the two cities. 4:00 p.m. in Los Angeles occurs after 5:00 p.m. on the same day in New York. When you specify time zones, comparisons of datetime
arrays account for the time zone information of each array.
TF = NYtime < LAtime
TF = logical
1
Other Comparisons
MATLAB provides other functions for date and time comparisons.
isbetween
— Determine ifdatetime
orduration
values are within an intervalisdst
— Determine ifdatetime
values occur during daylight saving timeisnat
— Determine ifdatetime
values areNaT
(Not-a-Time) valuesisweekend
— Determine ifdatetime
values occur during a weekend (Saturday and Sunday)
For example, determine if any elements of a datetime
array occur during the first quarter of 2022.
start1Q = datetime("2022-01-01"); end1Q = datetime("2022-04-01"); D = datetime(2022,2:4:10,15,12,0,0)
D = 1x3 datetime
15-Feb-2022 12:00:00 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00
TF = isbetween(D,start1Q,end1Q)
TF = 1x3 logical array
1 0 0
Display the elements of D
that occur during the first quarter.
D(TF)
ans = datetime
15-Feb-2022 12:00:00
Specify the time zone of D
by setting its TimeZone
property. Then determine if any elements occur during daylight saving time.
D.TimeZone = "America/New_York";
isdst(D)
ans = 1x3 logical array
0 1 1
Determine if any elements occur during a weekend.
isweekend(D)
ans = 1x3 logical array
0 0 1
To show the day of the week of the matching elements, use the day
function.
weekendDays = D(isweekend(D))
weekendDays = datetime
15-Oct-2022 12:00:00
day(weekendDays,"name")
ans = 1x1 cell array
{'Saturday'}
See Also
datetime
| duration
| isbetween
| isdst
| isnat
| isweekend
| day
| month