Re: NSDate isEqual to...
Re: NSDate isEqual to...
- Subject: Re: NSDate isEqual to...
- From: mm w <email@hidden>
- Date: Fri, 20 Mar 2009 14:21:15 -0700
+ (BOOL)isADayEqualToAnotherDay:(NSDate*)date anotherDate:(NSDate*)anotherDate
{
NSCalendar *cal;
NSDateComponents *componentsFromDate, *componentsFromAnotherDate;
NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit;
cal = [NSCalendar currentCalendar];
componentsFromDate = [cal components:unitFlags fromDate:date];
componentsFromAnotherDate = [cal components:unitFlags fromDate:anotherDate];
return (
[componentsFromDate year] == [componentsFromAnotherDate year] &&
[componentsFromDate month] == [componentsFromAnotherDate month] &&
[componentsFromDate day] == [componentsFromAnotherDate day]
);
}
+ (NSDate *)timeWithInterval:(NSInteger)timeInterval
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSCalendar *cal = [[[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *result;
[comps setHour:(timeInterval / 3600)];
[comps setMinute:(timeInterval / 60) - ((timeInterval / 3600) * 60)];
[comps setSecond:(timeInterval % 60)];
result = [cal dateFromComponents:comps];
[comps release];
return result;
}
(those are only sample codes there are better ways to handle this, I
only pointed you some methods, that will let you the opportunity to
digg into NSDate/NSIntervalTime/NSCalendar APIs, and reach your way)
what you need to compare: is the time of the dateTime object,
Cheers!
On Fri, Mar 20, 2009 at 2:05 PM, Mike Abdullah
<email@hidden> wrote:
>
> 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
>
--
-mmw
_______________________________________________
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