【问题标题】:Check whether two Dates are equal to minute precision检查两个日期是否等于分钟精度
【发布时间】:2017-11-12 16:18:57
【问题描述】:

我想检查两个日期是否相等,其中相等意味着它们具有相同的分钟值。我不想比较秒值。

例如:

// should be equal
11/12/17 11:19:29
11/12/17 11:19:03

// not equal
11/12/17 11:19:59
11/12/17 11:20:00

我正在考虑的一种方法是获取时间组件并遍历每个组件。但我想知道是否有更快或更清洁的方法。

【问题讨论】:

标签: ios swift cocoa-touch nsdate nscalendar


【解决方案1】:

Calendar 类为此提供了compare(_:to:toGranularity:) 方法:

let result = Calendar.current.compare(date1, to: date2, toGranularity: .minute)

result 将指示 date1 是 >、= 还是

【讨论】:

    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多