开发者

date Manipulation in iphone sdk?

i am using following 开发者_Python百科code to compare two dates only(not time) and also with time, but it returns always future …any help please?how can i alter the following code to do it?

   NSComparisonResult result = [[NSDate date] compare:[NSDate date]];

        switch (result)
        {
            case NSOrderedAscending: 
                NSLog(@" in future"); 


                break;
            case NSOrderedDescending: 
                NSLog(@" in past");
                break;
            case NSOrderedSame:
                NSLog(@" in same");
               break;
            }


[NSDate date] will create new date object using current date and time. Since you are creating two objects of NSDate at different times (though only a difference of milliseconds), thats why it is returning in future always.

I can think of two ways to fix this.

Create date object only ones:

NSDate *currentDate = [NSDate date];
NSComparisonResult result = [currentDate compare:currentDate];

Or You can use NSTimeInterval which returns time differnce in seconds:

(NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜