• 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: mmalc Crawford <email@hidden>
  • Date: Tue, 05 Jan 2010 13:23:52 -0800

On Jan 5, 2010, at 1:02 pm, Quincey Morris wrote:

> The OP wanted to use NSDate objects to avoid the clunkiness of multiple values/variables with NSDateComponents. I'm arguing that avoiding NSDateComponents is a *lot* more work, and that using NSDate for this purpose is *very* easy to get wrong.
>
The issue with NSDateComponents is that they must be understood in the context of a particular calendar.

An NSDate object represent a single point in time -- you can think of it basically as a wrapper for an NSTimeInterval from the reference date.  If you want to create components from the date, then you must do so with respect to a particular calendar *and time zone*...  This is of course possible, but then you have to be careful about always using the same combination of calendar and time zone to create the components and recreate the date from the components.

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];

    NSDateComponents *components1 = [[NSDateComponents alloc] init];
    [components1 setWeekday:2];
    [components1 setWeekdayOrdinal:1];
    [components1 setMonth:5];
    [components1 setYear:2008];
    NSDate *date1 = [gregorian dateFromComponents:components1];

    NSLog(@"date1: %@", date1);

    [gregorian setTimeZone:[NSTimeZone timeZoneWithName:@"Pacific/Fiji"]];
    date1 = [gregorian dateFromComponents:components1];
    NSLog(@"date1: %@", date1);


mmalc

_______________________________________________

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: Kyle Sluder <email@hidden>
References: 
 >NSDate without time portion (From: Brian Bruinewoud <email@hidden>)
 >Re: NSDate without time portion (From: Quincey Morris <email@hidden>)
 >Re: NSDate without time portion (From: Chris Ridd <email@hidden>)
 >Re: NSDate without time portion (From: David Duncan <email@hidden>)
 >Re: NSDate without time portion (From: Quincey Morris <email@hidden>)
 >Re: NSDate without time portion (From: Nick Zitzmann <email@hidden>)
 >Re: NSDate without time portion (From: Quincey Morris <email@hidden>)

  • Prev by Date: NSSavePanel with accessory view and file types popup
  • Next by Date: Re: Resolution Independence
  • Previous by thread: Re: NSDate without time portion
  • Next by thread: Re: NSDate without time portion
  • Index(es):
    • Date
    • Thread