Cocoa-Touch: How do I see if two NSDates are in the same day?

后端 未结 12 617
走了就别回头了
走了就别回头了 2020-11-30 04:35

I need to know if two NSDate instances are both from the same day.

Is there an easier/better way to do it than getting the NSDateComponents and comparing day/month/y

相关标签:
12条回答
  • 2020-11-30 05:04

    in Swift 3.0, iOS8+

    if NSCalendar.current.isDate(Date(), equalTo: date, toGranularity: .day) {
    
    }
    

    or

    if NSCalendar.current.isDateInToday(date) {
    
    }
    
    0 讨论(0)
  • 2020-11-30 05:09

    If you are working with iOS 8 you can use -isDate:inSameDayAsDate:.

    From NSCalendar.h:

    /*
        This API compares the Days of the given dates, reporting them equal if they are in the same Day.
    */
    - (BOOL)isDate:(NSDate *)date1 inSameDayAsDate:(NSDate *)date2 NS_AVAILABLE(10_9, 8_0);
    

    Note that for some reason this didn't make it to the official documentation on Apple's developer site. But it is definitely part of the public API.

    0 讨论(0)
  • 2020-11-30 05:11

    NSDateComponents sounds like the best bet to me. Another tactic to try is toll-free-bridging it to a CFDate, then using CFDateGetAbsoluteTime and doing a subtraction to get the amount of time between the two dates. You'll have to do some additional math to figure out if the time difference lands the dates on the same day, however.

    0 讨论(0)
  • 2020-11-30 05:12
    NSCalendar.currentCalendar().isDateInToday(yourNSDate)
    

    Available in iOS 8.0 and later and OS X v10.9 and later.

    0 讨论(0)
  • 2020-11-30 05:15

    The following will test whether two dates represent the same day in a given era (which is sufficient in many cases):

    - (BOOL)isDate:(NSDate *)date1 sameDayAsDate:(NSDate *)date2 {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        int diff = [calendar ordinalityOfUnit:NSDayCalendarUnit 
                                       inUnit:NSEraCalendarUnit 
                                      forDate:date1] -
                   [calendar ordinalityOfUnit:NSDayCalendarUnit 
                                       inUnit:NSEraCalendarUnit 
                                      forDate:date2];
        return 0 == diff;
    }
    
    0 讨论(0)
  • 2020-11-30 05:17

    I create my own utility classes.

    ZYUtility.h

    @interface ZYUtility : NSObject
    + (NSDate *)yesterday;
    + (NSDate *)tomorrow;
    + (NSDate *)endOfDay:(NSDate *)date;
    + (NSDate *)beginningOfDay:(NSDate *)date;
    @end
    

    ZYUtility.m

    + (NSDate *)yesterday;
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *componets = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                  fromDate:[NSDate date]];
        componets.day -= 1;
        componets.hour = 24;
        componets.minute = 0;
        componets.second = 0;
    
        return [calendar dateFromComponents:componets];
    }
    
    + (NSDate *)tomorrow;
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *componets = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                  fromDate:[NSDate date]];
        componets.day += 1;
        componets.hour = 0;
        componets.minute = 0;
        componets.second = 0;
    
        return [calendar dateFromComponents:componets];
    }
    
    + (NSDate *)beginningOfDay:(NSDate *)date {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
        NSDateComponents *comp = [calendar components:unitFlags fromDate:date];
    
        return [calendar dateFromComponents:comp];
    }
    
    + (NSDate *)endOfDay:(NSDate *)date {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *components = [NSDateComponents new];
        components.day = 1;
    
        NSDate *theDate = [calendar dateByAddingComponents:components
                                                    toDate:[ZYUtility beginningOfDay:date]
                                                   options:0];
    
        theDate = [theDate dateByAddingTimeInterval:-1];
    
        return theDate;
    }
    
    0 讨论(0)
提交回复
热议问题