Comparing NSDates without time component

后端 未结 17 1753
轮回少年
轮回少年 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条回答
  •  旧时难觅i
    2020-11-27 13:07

    To answer your question:

    Is this possible in Swift?

    Yes, it is possible


    Ahh, you also want to now HOW

    let cal = NSCalendar.currentCalendar()
    cal.rangeOfUnit(.DayCalendarUnit, startDate: &d1, interval: nil, forDate: d1) // d1 NSDate?
    cal.rangeOfUnit(.DayCalendarUnit, startDate: &d2, interval: nil, forDate: d2) // d2 NSDate?
    

    Now d1 and d2 will contain the dates at beginning of their days.

    compare with d1!.compare(d2!)

    To display them without time portion, us NSDateFormatter.

提交回复
热议问题