[英]How to determine date falls in between two dates swift
如何確定日期介於兩個日期值之間?
我的工作時間 startDate = 2021-01-27 09:00:00 +0000
& EndDate = 2021-01-27 18:00:00 +0000
如何驗證休息時間介於工作時間段之間?
如果你有 Date 對象,它們是 Comparable 和 Equatable 的。 您可以編寫如下代碼:
if breakDate >= startDate && breakDate <= endDate then {
// breakDate is between startDate and endDate
}
正如 LeoDabus 在評論中指出的那樣,您也可以使用模式匹配運算符(盡管我個人不喜歡它)
if startDate ... endDate ~= breakDate then {
// breakDate is between startDate and endDate
}
或包含:
if (startDate...endDate).contains(breakDate) then {
// breakDate is between startDate and endDate
}
如果您有日期字符串,請創建一個將日期字符串轉換為Date
對象的日期格式化程序,然后使用上述代碼。
如果日期字符串始終采用“Internet”日期格式(ISO 8601),並且您確定它們將始終處於同一時區,您還可以使用字符串比較並直接比較日期字符串(使用上述 forms比較,除了可能包含?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.