kheraud
kheraud

Reputation: 5288

How to know if two NSDate are in the same day

Do you know how to know if two NSDate are the same day. I want to take into account the locale...

It could be easy to use a timeIntervalSinceDate: but Monday 23H58 and Tuesday 00H01 are not in the same day...

Dealing with NSDate and locale for calculation is not very easy.

Upvotes: 7

Views: 7307

Answers (7)

rene
rene

Reputation: 2124

Since iOS 8 this is straightforward:

let isSameDay = NSCalendar.currentCalendar().isDate(date1, inSameDayAsDate: date2)

and it becomes a little simpler yet with Swift 3:

let isSameDay = Calendar.current.isDate(date1, inSameDayAs: date2)

Upvotes: 10

Terry Wilcox
Terry Wilcox

Reputation: 9040

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *componentsForFirstDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:firstDate];

NSDateComponents *componentsForSecondDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:secondDate];

if ([componentsForFirstDate year] == [componentsForSecondDate year])

etc.

I don't know if isEquals would do what you want on NSDateComponents.

Upvotes: 18

Alexander Volkov
Alexander Volkov

Reputation: 8407

Swift:

func isSameDays(date1:NSDate, _ date2:NSDate) -> Bool {
    let calendar = NSCalendar.currentCalendar()
    var comps1 = calendar.components([NSCalendarUnit.Month , NSCalendarUnit.Year , NSCalendarUnit.Day], fromDate:date1)
    var comps2 = calendar.components([NSCalendarUnit.Month , NSCalendarUnit.Year , NSCalendarUnit.Day], fromDate:date2)

    return (comps1.day == comps2.day) && (comps1.month == comps2.month) && (comps1.year == comps2.year)
}

Upvotes: 6

Yiming Tang
Yiming Tang

Reputation: 557

If your app is going to running in OS X 10.9+ or iOS 8+ only, you can use the new APIs of NSCalender.

/*
    This API compares the given dates down to the given unit, reporting them equal if they are the same in the given unit and all larger units.
*/
- (BOOL)isDate:(NSDate *)date1 equalToDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);

There are also a lot of convenient APIs. However, Apple hasn't mentioned them in their doc set. Anyway, the APIs are public and you can use them.

Upvotes: 1

Bobo Shone
Bobo Shone

Reputation: 721

Set timeZone if the date is not system locale.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:self.cityData.timeZone];
[calendar setTimeZone:timeZone];
NSDateComponents *componentsForFirstDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:weatherDataDate];
NSDateComponents *componentsForSecondDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:yesterday];            
if ([componentsForFirstDate day] == [componentsForSecondDate day]) {
    // TODO: 
} 

Upvotes: 0

oqu
oqu

Reputation: 382

Do it with NSDateFormatter :

NSDateFormatter *_df = [[NSDateFormatter alloc] init];
_df.dateFormat = @"yyyy.MM.dd";
NSString *_d1 = [_df stringFromDate:_date1];
NSString *_d2 = [_df stringFromDate:_date2];
if ([_d1 isEqualToString:_d2] == YES)
{
    // d1 and d2 is on same day/month/year
}
[_df release];

Upvotes: 5

Vladimir
Vladimir

Reputation: 170859

Use NSCalendar and NSDateComponents for that:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps1 = [cal components:(NSMonthCalendarUnit| NSYearCalendarUnit | NSDayCalendarUnit) 
                                      fromDate:date1];
NSDateComponents *comps2 = [cal components:(NSMonthCalendarUnit| NSYearCalendarUnit | NSDayCalendarUnit) 
                                      fromDate:date2];


BOOL sameDay = ([comps1 day] == [comps2 day] 
                  && [comps1 month] == [comps2 month] 
                  && [comps1 year] == [comps2 year]);

Upvotes: 8

Related Questions