Swift 5:计算当前时间在2倍之间

Swift 5: Calculate current time is between 2 times

我不擅长Swift所以我想知道如何计算当前时间在两次之间

我从后端收到以下响应。 {"workHours":"M-F 9:00 - 18:00"}

从上面的字符串中,如何写一个class/struct(或其他最佳方式)来验证今天的当前时间是否在描述的时间范围内("M-F 9:00 - 18:00")?

public final class DateValidator {
    let startTime: Date?
    let endTime: Date?
    
    func isInRange() -> Bool {
        // write modules here
        // How to validate
    }
    
    public init(dateString: String) {
        // dateString should be "M-F 9:00 - 18:00"
        // Extract startTime and endTime from dateString
    }
}

设置 DateComponentsFormatter

let dcf = DateComponentsFormatter()
dcf.allowedUnits = [.day, .hour, .minute, .second]
dcf.unitsStyle = .full

设置普通日期格式化程序

let df = ISO8601DateFormatter()
df.formatOptions = [.withFullDate, .withDashSeparatorInDate]

执行格式化

if let future = df.date(from: "2019-04-29"), let diff = dcf.string(from: Date(), to: future) {
    print(diff)
}

输出为

3 天 6 小时 9 分 9 秒