Iphone 如何比较两个NSDATE:哪个是最新的?
我正在尝试实现dropBox同步,需要比较两个文件的日期。一个在我的dropBox帐户上,一个在我的iPhone上 我得出了以下结论,但我得到了意想不到的结果。我想我在比较这两个日期时犯了一些根本错误。我只是简单地使用了><运算符,但我想这是不好的,因为我正在比较两个NSDate字符串。我们开始:Iphone 如何比较两个NSDATE:哪个是最新的?,iphone,objective-c,cocoa-touch,nsdate,Iphone,Objective C,Cocoa Touch,Nsdate,我正在尝试实现dropBox同步,需要比较两个文件的日期。一个在我的dropBox帐户上,一个在我的iPhone上 我得出了以下结论,但我得到了意想不到的结果。我想我在比较这两个日期时犯了一些根本错误。我只是简单地使用了>
NSLog(@"dB...lastModified: %@", dbObject.lastModifiedDate);
NSLog(@"iP...lastModified: %@", [self getDateOfLocalFile:@"NoteBook.txt"]);
if ([dbObject lastModifiedDate] < [self getDateOfLocalFile:@"NoteBook.txt"]) {
NSLog(@"...db is more up-to-date. Download in progress...");
[self DBdownload:@"NoteBook.txt"];
NSLog(@"Download complete.");
} else {
NSLog(@"...iP is more up-to-date. Upload in progress...");
[self DBupload:@"NoteBook.txt"];
NSLog(@"Upload complete.");
}
或者这个恰好是正确的:
2011-05-11 14:20:25.097 NotePage[6903:207] dB...lastModified: 2011-05-11 13:18:25 +0000
2011-05-11 14:20:25.098 NotePage[6903:207] iP...lastModified: 2011-05-11 13:19:45 +0000
2011-05-11 14:20:25.099 NotePage[6903:207] ...iP is more up-to-date.
让我们假设两个日期:
NSDate *date1;
NSDate *date2;
然后,通过以下比较,可以看出哪个较早/较晚/相同:
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
} else {
NSLog(@"dates are the same");
}
有关更多详细信息,请参阅。您应该使用:
- (NSComparisonResult)compare:(NSDate *)anotherDate
比较日期。目标C中没有运算符重载。您希望使用NSDate比较:、laterDate:、earlierDate:、或isEqualToDate:方法。在这种情况下,使用<和>运算符是比较指针,而不是日期
NSDate
具有比较功能
compare:
返回一个NSComparisonResult
值,该值指示接收器和另一个给定日期的时间顺序
(NSComparisonResult)compare:(NSDate *)anotherDate
参数:其他日期
与接收器进行比较的日期。
此值不能为零。如果该值为nil,则行为未定义,可能会在未来版本的Mac OS X中更改
返回值:
- 如果接收者和另一个日期完全相等,
sensorderedname
- 如果接收器的时间晚于另一个日期,
sensorderedescending
- 如果接收器的时间早于另一个日期,
sensorderedascing
这将返回接收方和其他日期中较早的一个。如果两者相同,则返回接收者。我遇到了几乎相同的情况,但在我的例子中,我正在检查天数是否存在差异
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *compDate = [cal components:NSDayCalendarUnit fromDate:fDate toDate:tDate options:0];
int numbersOfDaysDiff = [compDate day]+1; // do what ever comparison logic with this int.
当您需要以天/月/年为单位比较NSDate时非常有用我已经尝试过,希望对您有用
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int unitFlags =NSDayCalendarUnit;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *myDate; //= [[NSDate alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
myDate = [dateFormatter dateFromString:self.strPrevioisDate];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:myDate toDate:[NSDate date] options:0];
NSInteger day=[comps day];
迟到,但比较NSDate对象的另一种简单方法是将它们转换为原始类型,这样就可以方便地使用'>''您也可以通过此方法比较两个日期。
switch ([currenttimestr compare:endtimestr])
{
case NSOrderedAscending:
// dateOne is earlier in time than dateTwo
break;
case NSOrderedSame:
// The dates are the same
break;
case NSOrderedDescending:
// dateOne is later in time than dateTwo
break;
}
一些日期实用程序,包括英文比较,这很好:
#import <Foundation/Foundation.h>
@interface NSDate (Util)
-(BOOL) isLaterThanOrEqualTo:(NSDate*)date;
-(BOOL) isEarlierThanOrEqualTo:(NSDate*)date;
-(BOOL) isLaterThan:(NSDate*)date;
-(BOOL) isEarlierThan:(NSDate*)date;
- (NSDate*) dateByAddingDays:(int)days;
@end
你们为什么不使用这些
NSDate
比较方法:
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
在Swift中,可以重载现有运算符:
func > (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate
}
func < (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}
func>(左:NSDate,右:NSDate)->Bool{
返回lhs.timeIntervalSinceReferenceDate>rhs.timeIntervalSinceReferenceDate
}
func<(左:NSDate,右:NSDate)->Bool{
返回lhs.timeintervalncereferencedate
然后,您可以直接将NSDATE与
和=
(已支持)进行比较。使用此简单函数进行日期比较
-(BOOL)dateComparision:(NSDate*)date1 andDate2:(NSDate*)date2{
BOOL isTokonValid;
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
isTokonValid = YES;
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
isTokonValid = NO;
} else {
isTokonValid = NO;
NSLog(@"dates are the same");
}
return isTokonValid;}
复制品:@JoshCaswell如果是真的复制品,为什么不合并它们?你以前做过…只有钻石版主才能执行合并,@Yar.Lovely!比摆弄[date1 earlierDate:date2]等更好。。。谢谢-出于某种原因,我以前从来没有想过使用compare:before。我喜欢依赖这样一个事实:sensorderedescing<0和sensorderedescing>0。这使得比较更容易阅读:[date1 compare:date2]<0/*date1
date1->date2
是升序/降序的(因此date1是更晚或更早的)。@Jack这是一种更抽象的方式,没有神奇的数字(-1,0,1),排序算法可以将元素按顺序排列。您还可以自己用更可读的名称重新定义这些常量。我的答案是做这项工作,但不是可读代码的获奖者。向下滚动,还有其他好的。@Irene有没有办法比较两个只有时间成分不同的NSDate对象?出于某些原因,上述方法不起作用。或者只使用:github.com/erica/NSDate-extensions请注意:比(NSComparisonResult)compare:(NSDate*)
稍微直观一些,但对于这个简单的操作来说仍然非常详细。。。(通常)也可以执行[dateA TimeIntervalsIncestate:dateB]>0
注意,NSDates的备份对象可以在编译代码的64位版本上进行优化,以便表示相同时间的日期具有相同的地址。因此,如果cDate=[aDate earlierDate:bDate]
那么cDate==aDate
和cDate==bDate
都可以为真。在iOS 8上执行一些日期工作时发现此问题。相反,在32位平台上,如果日期不相同,-earlierDate:
(和-laterDate:
)既不能返回接收者也不能返回参数。实际上,我先前关于32位平台的评论是不正确的-earlierDate:
和-laterDate:
返回参数或接收器。但是,NSManagedObject
s上的NSDate
属性将为每个调用返回不同的实例。NSDayCalendarUnit已被弃用,因此请使用NSCalendarUnitDay代替。如果我尝试对此进行扩展,我会得到“运算符仅允许在全局范围内使用”,建议?@JohnVanDijk您不能将其放入扩展中。我把它放在与扩展名相同的文件中,但在{…}
#import "NSDate+Util.h"
@implementation NSDate (Util)
-(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);
}
- (NSDate *) dateByAddingDays:(int)days {
NSDate *retVal;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
retVal = [gregorian dateByAddingComponents:components toDate:self options:0];
return retVal;
}
@end
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
func > (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate > rhs.timeIntervalSinceReferenceDate
}
func < (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
}
-(BOOL)dateComparision:(NSDate*)date1 andDate2:(NSDate*)date2{
BOOL isTokonValid;
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(@"date1 is later than date2");
isTokonValid = YES;
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(@"date1 is earlier than date2");
isTokonValid = NO;
} else {
isTokonValid = NO;
NSLog(@"dates are the same");
}
return isTokonValid;}