Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS comparing two same NSDates does not return NSOrderedSame

Tags:

ios

nsdate

As the title, I have tried the following simple code:

NSDate *date1 = [NSDate date];
NSDate *date2 = [NSDate date];
if([date1 compare:date2] == NSOrderedSame)
 NSLog(@"two time same");
else
 NSLog(@"two time not same");

and the result is:

two time not same

I don't understand that, can someone give me some tips? Thank you.

like image 659
user3804862 Avatar asked Jan 28 '26 17:01

user3804862


1 Answers

it's not same because there is a little time difference of creation of object, just try to print there value in NSTimeInterVal to know better.

Example:

NSDate *date1 = [NSDate date];
NSDate *date2 = [NSDate date];

NSLog(@"date1 : %f", [date1 timeIntervalSince1970]);
NSLog(@"date2 : %f", [date2 timeIntervalSince1970]);

Output:

date1 : 1404466511.638555
date2 : 1404466511.638558

and you also can see the difference using method

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
like image 112
Retro Avatar answered Jan 30 '26 05:01

Retro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!