Detect if two dates are within the same calendar day

My application provides a list of messages, each of which has a creation date.

I don’t want to just show the date, but show different formats depending on how much time has passed since the creation date.

For example, this is how I create a date string from a date:

NSDate *date = someMessage.creationDate; NSTimeInterval timePassed = [[NSDate date] timeIntervalSinceDate:date]; if (timePassed < 60 ) { // less then one minute return @"now" } else if (timePassed < 3600) { // less then one hour NSInteger minutes = (NSInteger) floor(timePassed / 60.0); return [NSString stringWithFormat:@"%d minutes ago", minutes]; } 

Now I want to add the following cases:

 else if ("timePassed < 24 hours ago and within the same calendar day") // do something } else if ("timePassed > 24 hours, or within previous calendar day") { // do something else } 

but not sure how to do this, any help would be appreciated

+6
source share
2 answers

Here's a simple category on NSDate that adds useful date comparison methods:

 #ifndef SecondsPerDay #define SecondsPerDay 86400 #endif @interface NSDate (Additions) /** * This method returns the number of days between this date and the given date. */ - (NSUInteger)daysBetween:(NSDate *)date; /** * This method compares the dates without time components. */ - (NSComparisonResult)timelessCompare:(NSDate *)date; /* * This method returns a new date with the time set to 12:00 AM midnight local time. */ - (NSDate *)dateWithoutTimeComponents; @end @implementation NSDate (Additions) - (NSUInteger)daysBetween:(NSDate *)date { NSDate *dt1 = [self dateWithoutTimeComponents]; NSDate *dt2 = [date dateWithoutTimeComponents]; return ABS([dt1 timeIntervalSinceDate:dt2] / SecondsPerDay); } - (NSComparisonResult)timelessCompare:(NSDate *)date { NSDate *dt1 = [self dateWithoutTimeComponents]; NSDate *dt2 = [date dateWithoutTimeComponents]; return [dt1 compare:dt2]; } - (NSDate *)dateWithoutTimeComponents { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:self]; return [calendar dateFromComponents:components]; } @end 

Usage example:

 NSDate *currentDate = [NSDate date]; NSDate *distantPastDate = [NSDate distantPast]; NSComparisonResult *result = [currentDate timelessCompare:distantPastDate]; // result will equal NSOrderedDescending 

Need to know a more detailed time difference?

Do you need to know the difference between two dates before the second? Use timeIntervalSinceDate: which is standard on NSDate .

+7
source

You want to see this answer .

Namely:

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:yourDate]; NSUInteger year = [components year]; NSUInteger dayOfYear = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:yourDate]; 

Check that the day attribute is for both NSDates, then you will find out if they were on the same calender day. As for 24 hours or less / more, you can take one date and use the timeIntervalSinceDate: function and see if it is more or less 86400 (number of seconds per day).

EDIT: Updated to be a more specific check for year and day. One.

+1
source

All Articles