Comparison of iOS dates
1. dates can be compared to determine the size or equality, or to determine the time interval between two dates. You can use the-timeIntervalSinceDate method to calculate the time difference between two dates.
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSTimeInterVal timeBetween = [now timeIntervalSinceDate: anHourAgo];
- NSLog (@ "% f", timeBetween );
2. You can use the-timeIntervalSinceNow method to obtain the date comparison and the current time interval.
- NSDate * anHourago = [NSDate dateWithTimeIntervalSinceNow;-60*60];
- NSTimeInterval interval = [anHourAgo timeIntervalSinceNow];
- NSLog (@ "% f", interval );
// Create Date Format object NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@ "yyyy-MM-dd HH:mm" ]; // Two date objects are created. NSDate *date1=[dateFormatter dateFromString:@ "2010-3-3 11:00" ]; NSDate *date2=[dateFormatter dateFromString:@ "2010-3-4 12:00" ]; //NSDate *date=[NSDate date]; //NSString *curdate=[dateFormatter stringFromDate:date]; // Take the time interval of two date objects: // Here NSTimeInterval is not an object, it is a basic type, in fact, it is a double type, which is defined by c: typedef double NSTimeInterval; NSTimeInterval time =[date2 timeIntervalSinceDate:date1]; int days=(( int ) time )/(3600*24); int hours=(( int ) time )%(3600*24)/3600; NSString *dateContent=[[NSString alloc] initWithFormat:@ "% I day % I hour" ,days,hours]; |
3. NSDate also provides the-laterDate,-earlierDate, and compare methods to compare dates.
- NSDate * now = [NSDate date];
- NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
- NSDate * result1 = [now laterDate: anHourAgo];
- NSDate * result2 = [now earlierDate: anHourAgo];
- NSComparisonResult result3 = [now compare: anHourAgo];
Comparing the date size is a common problem in any programming language. In iOS programming, the NSDate object is usually used to store a time (including the date, time, and time zone ), in addition, the NSDate class provides the compare method to compare the time, but sometimes you do not want to know the size of the two dates so accurately (the default value is the second). You can use the following implementation method:
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy"]; NSString *oneDayStr = [dateFormatter stringFromDate:oneDay]; NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay]; NSDate *dateA = [dateFormatter dateFromString:oneDayStr]; NSDate *dateB = [dateFormatter dateFromString:anotherDayStr]; NSComparisonResult result = [dateA compare:dateB]; NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay); if (result == NSOrderedDescending) { //NSLog(@"Date1 is in the future"); return 1; } else if (result == NSOrderedAscending){ //NSLog(@"Date1 is in the past"); return -1; } //NSLog(@"Both dates are the same"); return 0; }