How to determine if an NSDate is today?

前端 未结 20 2088
礼貌的吻别
礼貌的吻别 2020-11-28 18:20

How to check if an NSDate belongs to today?

I used to check it using first 10 characters from [aDate description]. [[aDate descriptio

20条回答
  •  醉酒成梦
    2020-11-28 19:06

    In macOS 10.9+ & iOS 8+, there's a method on NSCalendar/Calendar that does exactly this!

    - (BOOL)isDateInToday:(NSDate *)date 
    

    So you'd simply do

    Objective-C:

    BOOL today = [[NSCalendar currentCalendar] isDateInToday:date];
    

    Swift 3:

    let today = Calendar.current.isDateInToday(date)
    

提交回复
热议问题