我有两个日期:2009-05-11
和当前日期.我想检查给定日期是否是当前日期.这怎么可能.
Cocoa有几种方法:
在NSDate
– isEqualToDate: – earlierDate: – laterDate: – compare:
当你使用时- (NSComparisonResult)compare:(NSDate *)anotherDate
,你会回来其中一个:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame The receiver is later in time than anotherDate, NSOrderedDescending The receiver is earlier in time than anotherDate, NSOrderedAscending.
例:
NSDate * now = [NSDate date]; NSDate * mile = [[NSDate alloc] initWithString:@"2001-03-24 10:45:32 +0600"]; NSComparisonResult result = [now compare:mile]; NSLog(@"%@", now); NSLog(@"%@", mile); switch (result) { case NSOrderedAscending: NSLog(@"%@ is in future from %@", mile, now); break; case NSOrderedDescending: NSLog(@"%@ is in past from %@", mile, now); break; case NSOrderedSame: NSLog(@"%@ is the same as %@", mile, now); break; default: NSLog(@"erorr dates %@, %@", mile, now); break; } [mile release];
伙计.此功能将使您的日期与任何指定日期相匹配,并告知它们匹配与否的天气.您还可以修改组件以符合您的要求.
- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 { NSCalendar* calendar = [NSCalendar currentCalendar]; unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1]; NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2]; return [comp1 day] == [comp2 day] && [comp1 month] == [comp2 month] && [comp1 year] == [comp2 year];}
此致,Naveed Butt
NSDate *today = [NSDate date]; // it will give you current date NSDate *newDate = [NSDate dateWithString:@"xxxxxx"]; // your date NSComparisonResult result; //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending result = [today compare:newDate]; // comparing two dates if(result==NSOrderedAscending) NSLog(@"today is less"); else if(result==NSOrderedDescending) NSLog(@"newDate is less"); else NSLog(@"Both dates are same");
您可以使用其他方法来比较NSDate对象.在某些任务中,每种方法都会更有效.我选择了比较方法,因为它可以处理大多数基本日期比较需求.
此类别提供了一种比较NSDates的简洁方法:
#import@interface NSDate (Compare) -(BOOL) isLaterThanOrEqualTo:(NSDate*)date; -(BOOL) isEarlierThanOrEqualTo:(NSDate*)date; -(BOOL) isLaterThan:(NSDate*)date; -(BOOL) isEarlierThan:(NSDate*)date; //- (BOOL)isEqualToDate:(NSDate *)date; already part of the NSDate API @end
并实施:
#import "NSDate+Compare.h" @implementation NSDate (Compare) -(BOOL) isLaterThanOrEqualTo:(NSDate*)date { return !([self compare:date] == NSOrderedAscending); } -(BOOL) isEarlierThanOrEqualTo:(NSDate*)date { return !([self compare:date] == NSOrderedDescending); } -(BOOL) isLaterThan:(NSDate*)date { return ([self compare:date] == NSOrderedDescending); } -(BOOL) isEarlierThan:(NSDate*)date { return ([self compare:date] == NSOrderedAscending); } @end
使用简单:
if([aDateYouWantToCompare isEarlierThanOrEqualTo:[NSDate date]]) // [NSDate date] is now { // do your thing ... }
如果你把两个日期NSDate
是你可以使用NSDate
的compare:
方法:
NSComparisonResult result = [Date2 compare:Date1]; if(result==NSOrderedAscending) NSLog(@"Date1 is in the future"); else if(result==NSOrderedDescending) NSLog(@"Date1 is in the past"); else NSLog(@"Both dates are the same");
您可以在这里查看文档.
通过这种方法,您还可以比较两个日期
NSDate * dateOne = [NSDate date]; NSDate * dateTwo = [NSDate date]; if([dateOne compare:dateTwo] == NSOrderedAscending) { }
我找到的最好的方法是检查给定日期和今天之间的差异:
NSCalendar* calendar = [NSCalendar currentCalendar]; NSDate* now = [NSDate date]; int differenceInDays = [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:date] - [calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:now];
根据Apple 日期和时间编程指南中的Calendrical Calculations清单13 [NSCalendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:myDate]为您提供自该时代开始以来的午夜数.通过这种方式,可以轻松检查日期是昨天,今天还是明天.
switch (differenceInDays) { case -1: dayString = @"Yesterday"; break; case 0: dayString = @"Today"; break; case 1: dayString = @"Tomorrow"; break; default: { NSDateFormatter* dayFormatter = [[NSDateFormatter alloc] init]; [dayFormatter setLocale:usLocale]; [dayFormatter setDateFormat:@"dd MMM"]; dayString = [dayFormatter stringFromDate: date]; break; } }