• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Comparing dates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Comparing dates


  • Subject: Re: Comparing dates
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Fri, 26 Sep 2003 12:15:57 +0100

On Friday, September 26, 2003, at 01:57 am, April Gendill wrote:

Ok...
How do I compare two different dates?
lets say, I have scheduled an even for September 30 2003, and I want to know if it is that date yet.. How do I do that I can't seem to get my head wrapped around the NSDate and NSCalanderDate classes

It's pretty straightforward; something like the following should give you some ideas:

NSCalendarDate *myDate = [NSCalendarDate dateWithYear:2003 month:9 day:30
hour:0 minute:0 second:0
timeZone:[NSTimeZone localTimeZone]]
NSTimeInterval interval = [myDate timeIntervalSinceNow];

if (interval > 0)
NSLog (@"It isn't %@ yet.\n", myDate);
else if (interval > -60 * 60 * 24 /* number of seconds in a day */)
NSLog (@"It's %@ at the moment.\n", myDate);
else
NSLog (@"%@ is now in the past.\n", myDate);

The usual caveats apply as I just typed this code straight into Mail, but I think you get the basic idea. Note that -timeIntervalSinceNow is +ve if the NSCalendarDate you send it to is *after* the current date, zero if they are equal, and -ve otherwise.

If you need to compare a date other than the present one, then you use -timeIntervalSinceDate: instead of -timeIntervalSinceNow.

Kind regards,

Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Comparing dates (From: April Gendill <email@hidden>)

  • Prev by Date: Re: stringByAppendingBlahBlah woes
  • Next by Date: Re: nextKeyView broken
  • Previous by thread: Comparing dates
  • Next by thread: Re: Comparing dates
  • Index(es):
    • Date
    • Thread