• 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: NSDate without time portion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDate without time portion


  • Subject: Re: NSDate without time portion
  • From: Chris Ridd <email@hidden>
  • Date: Tue, 05 Jan 2010 19:23:59 +0000

On 4 Jan 2010, at 13:50, Quincey Morris wrote:

> On Jan 4, 2010, at 02:26, Brian Bruinewoud wrote:
>
>> What's the best way to get an NSDate object for 'today' such that the time is 00:00:00 (or any other constant).
>> I not interested in the time, I only care about the year-month-day, but I do need the the hours-minutes-seconds to be the same on all dates so that I can compare the dates.
>>
>> Currently I do this:
>>
>>   NSDateFormatter *dateFmter = [[NSDateFormatter alloc] init];
>>   [dateFmter setTimeStyle:NSDateFormatterNoStyle];
>>   [dateFmter setDateStyle:NSDateFormatterMediumStyle];
>>
>>   NSString dateText = [ dateFmter stringFromDate: self.now ]; // !! !! I need dateText anyway
>>
>>   self.now = [ dateFmter dateFromString: dateText ]; // !! truncate time to 00:00:00
>>
>> But this seems ugly, cumbersome and inefficient.
>>
>> The other option might be to use NSDate, NSCalendar and NSDateComponents, but that seems to be even more ugly and cumbersome and probably more inefficient.
>
> NSDate is *not* a good choice for these sorts of comparisons, because it's always a date and a time, and it's not as simple as it seems. Consider this (unlikely) example:

However Core Data models "dates" using NSDate. If you needed to model dates without times in Core Data (and be able to sort/filter on them) what would you do?

Cheers,

Chris
_______________________________________________

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

  • Follow-Ups:
    • Re: NSDate without time portion
      • From: "Sean McBride" <email@hidden>
    • Re: NSDate without time portion
      • From: Nick Zitzmann <email@hidden>
    • Re: NSDate without time portion
      • From: Robert Claeson <email@hidden>
    • Re: NSDate without time portion
      • From: David Duncan <email@hidden>
References: 
 >NSDate without time portion (From: Brian Bruinewoud <email@hidden>)
 >Re: NSDate without time portion (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: iPhone: @protocol and @optional
  • Next by Date: Re: NSDate without time portion
  • Previous by thread: Re: NSDate without time portion
  • Next by thread: Re: NSDate without time portion
  • Index(es):
    • Date
    • Thread