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
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.
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.
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.
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.
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
NSDate *currentDate = [NSDate date];
NSDate *distantPastDate = [NSDate distantPast];
NSComparisonResult *result = [currentDate timelessCompare:distantPastDate];
// result will equal NSOrderedDescending
Do you need to know the difference between two dates down to the second? Use timeIntervalSinceDate:
which comes standard on NSDate
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With