# 比较日期时间

• `datetime` 值可以在另一个 `datetime` 值之前、同时或之后。

• `duration` 值可以短于、等于或长于另一个 `duration` 值。

`calendarDuration` 数据类型不支持使用关系运算符进行比较。日历单位不一定表示固定的时间长度。

`datetime` 数组和 `duration` 数组无法进行比较。但是，您可以将 `datetime` 数组的分量与数字或 `duration` 数组进行比较。

### 比较 `datetime` 值

`d1 = datetime("2022-06-05 11:37:05")`
```d1 = datetime 05-Jun-2022 11:37:05 ```

`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 ```

`tf = d2 > d1`
```tf = 1x3 logical array 0 1 1 ```

`afterd1 = d2(tf)`
```afterd1 = 1x2 datetime 15-Jun-2022 12:00:00 15-Oct-2022 12:00:00 ```

### 文本与 `datetime` 值

`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 ```

### 数值与 `datetime` 数组的分量

`datetime` 数据类型支持访问 `datetime` 值的分量。使用 `year``quarter``month``day``hour``minute``second` 函数访问分量。您可以将分量与数字或 `duration` 值进行比较，因为这些函数返回数字。

`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 ```

`m = d2.Month`
```m = 1×3 2 6 10 ```

`tf = month(d2) < 6`
```tf = 1x3 logical array 1 0 0 ```
`beforeJune = d2(tf)`
```beforeJune = datetime 15-Feb-2022 12:00:00 ```

### 比较 `duration` 数组

`t1 = duration("03:37:12")`
```t1 = duration 03:37:12 ```

`t2 = duration(0:2:6,30,0)`
```t2 = 1x4 duration 00:30:00 02:30:00 04:30:00 06:30:00 ```

`tf = t2 > t1`
```tf = 1x4 logical array 0 0 1 1 ```

`longerThanT1 = t2(tf)`
```longerThanT1 = 1x2 duration 04:30:00 06:30:00 ```

### 文本与 `duration` 值

`tf = t2 >= "02:05:00"`
```tf = 1x4 logical array 0 1 1 1 ```
`longerThan205 = t2(tf)`
```longerThan205 = 1x3 duration 02:30:00 04:30:00 06:30:00 ```

### 数值数组与 `duration` 数组

`t2` 中的元素与一天进行比较。每个元素都较短。

`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 ```

`t2` 中的元素与一小时进行比较。只有 `t2` 中的第一个元素较短。

`tf = t2 < 1/24`
```tf = 1x4 logical array 1 0 0 0 ```
`t2(tf)`
```ans = duration 00:30:00 ```

### 比较不同时区的 `datetime` 数组

```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 ```

`tf = NYtime < LAtime`
```tf = logical 1 ```

```NYtime4 = datetime("2022-10-01 16:00:00", ... "TimeZone","America/New_York",... "Format","dd-MMM-yyyy hh:mm:ss a z")```
```NYtime4 = datetime 01-Oct-2022 04:00:00 PM EDT ```
`tf = NYtime4 == LAtime`
```tf = logical 0 ```

### 使用其他函数比较日期时间

MATLAB 还提供了其他用于日期时间比较的函数。

• `isbetween` - 确定 `datetime``duration` 数组的元素是否在一个区间内

• `isdst` - 确定 `datetime` 数组的元素是否在夏令时出现

• `isweekend` - 确定 `datetime` 数组的元素是否出现在周末（周六和周日）

• `ismissing` - 确定数组的元素是否为缺失值（对于 `datetime` 数组，缺失值为 `NaT`；对于 `duration` 数组，缺失值为 `NaN`

• `union` - 两个 `datetime` 或两个 `duration` 数组的并集

• `intersect` - 两个 `datetime` 或两个 `duration` 数组的交集

• `ismember` - 第一个 `datetime``duration` 数组中属于第二个 `datetime``duration` 数组中的元素

• `setdiff` - 两个 `datetime` 或两个 `duration` 数组的差集

• `setxor` - 两个 `datetime` 或两个 `duration` 数组的异或集

```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,"openright")`
```tf = 1x3 logical array 1 0 0 ```

`tf = (start1Q <= d & d < end1Q)`
```tf = 1x3 logical array 1 0 0 ```

`d(tf)`
```ans = datetime 15-Feb-2022 12:00:00 ```

```d.TimeZone = "America/New_York"; isdst(d)```
```ans = 1x3 logical array 0 1 1 ```

`isweekend(d)`
```ans = 1x3 logical array 0 0 1 ```

`weekendDays = d(isweekend(d))`
```weekendDays = datetime 15-Oct-2022 12:00:00 ```
`day(weekendDays,"name")`
```ans = 1x1 cell array {'Saturday'} ```