Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if two dates are within the same calendar day

My app presenting a list of messages, each has a creation date.

I don't want to just show the date but to show different formats according to how much time passed from the creation date.

for example, this is how I create the date string from the 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];
}

so 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 it, any help will be appreciated

like image 997
Mario Avatar asked Jan 26 '14 14:01

Mario


People also ask

How do you check if two dates are on the same day?

To check if two dates are the same day, call the toDateString() method on both Date() objects and compare the results. If the output from calling the method is the same, the dates are the same day.

How can I compare two dates in Java?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you check if two dates are equal in Python?

Use the datetime Module and the < / > Operator to Compare Two Dates in Python. datetime and simple comparison operators < or > can be used to compare two dates.

How do you compare two date functions?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.


1 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

Example Use:

NSDate *currentDate = [NSDate date];
NSDate *distantPastDate = [NSDate distantPast];

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

Need to know a more detailed difference in time?

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

like image 112
JRG-Developer Avatar answered Sep 22 '22 03:09

JRG-Developer