开发者

date Manipulation in iphone sdk?

开发者 https://www.devze.com 2023-01-29 14:26 出处:网络
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?

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

精彩评论

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