Re: NSDate isEqual to...
Re: NSDate isEqual to...
- Subject: Re: NSDate isEqual to...
- From: Mike Abdullah <email@hidden>
- Date: Fri, 20 Mar 2009 21:05:34 +0000
On 20 Mar 2009, at 20:50, Charles E. Heizer wrote:
Hello,
I'm playing around with date time stuff right now and I'm trying to
figure out the bets way to determine if one datetime is equal to
another. The problem I'm running in to is "isEqualToDate" does not
appear to work, the NSLog statement will show two identical datetime
statements but I never see a "These dates are the same!".
Can someone please tell me how I can get this to work.
Thanks,
Charles
NSDate *theDateTimeToRunAt = [NSDate dateWithString:@"2009-03-20
13:18:00 -0700"];
NSDate *theCurrentDateTime = [NSDate date];
while(1)
{
NSLog(@"theDateTime1=%@ | theCurrentDate=%@", theDateTimeToRunAt,
theCurrentDateTime);
if ([theDateTime1 isEqualToDate:theCurrentDate]) {
NSLog(@"These dates are the same!");
[theDateTimeToRunAt addTimeInterval:900]; // Add 15 Minutes to say
hi again!
}
sleep(1);
theDate = [NSDate date];
}
Because quite simply the dates are not exactly the same. NSDate is
based on NSTimeInterval which offers sub-second precision, and so it
is pretty unlikely that fetching the current time gives you a date
that is precisely 13:18. Instead, you want to compare the dates to see
if current date is greater than or equal to theDateTimeToRunAt.
Is this really your intention for the code though, or just an example
method? If the former, why not just use +[NSThread sleepUntilDate:] ?
Mike.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden