Comparing NSDates without time component

后端 未结 17 1690
轮回少年
轮回少年 2020-11-27 12:49

In a swift playground, I have been using

NSDate.date() 

But, this always appears with the time element appended. For my app I need to ignor

17条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 13:22

    Swift iOS 8 and up When you need more than simply bigger or smaller date comparisons. For example is it the same day or the previous day,...

    extension Date {
        func compareTo(date: Date, toGranularity: Calendar.Component ) -> ComparisonResult  {
            var cal = Calendar.current
            cal.timeZone = TimeZone(identifier: "Europe/Paris")!
            return cal.compare(self, to: date, toGranularity: toGranularity)
            }
        }
    

    For examples how to use this in a filter see this answer:

    https://stackoverflow.com/a/45746206/4946476

提交回复
热议问题