0

That's not a typo (at least not on my part, anyways.)

One of the defined NSCalendarUnits are a NSCalendarCalendarUnit. Can anyone explain what that is or how you would use it?

For example, I'd like to calculate the number of days between dates, which potentially took place in different eras. Apple provides this code for finding the number of days between dates within an era as such (it's implemented as a category on NSCalendar, thus self refers to an NSCalendar):

NSInteger start = [self ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:startDate];
NSInteger end   = [self ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:endDate];
return end - start;

Is the proper way of modifying this to accept the possibility of different eras by replacing NSEraCalendarUnit with NSCalendarCalendarUnit?

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • I just tried it out, replacing `NSEraCalendarUnit` with `NSCalendarCalendarUnit` is definitely not the right thing to do for what I want. The titular question remains, and I'd also like to know about how to write the code that I want. – ArtOfWarfare Jul 14 '13 at 18:58

1 Answers1

1

An instance of NSDateComponents can include a calendar. This component was added in iOS 4.0 and OS X 10.7:

iOS Note: In iOS 4.0 and later, NSDateComponents objects can contain a calendar, a timezone, and a date object. This allows date components to be passed to or returned from a method and retain their meaning.

You can pass NSCalendarCalendarUnit to -[NSCalendar components:fromDate:] to have it include the calendar in the returned NSDateComponents.

UPDATE

If you want to compute the number of days between two dates, use -[NSCalendar components:fromDate:toDate:options:]. You may need to construct the two NSDate objects first, using dateFromComponents:.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Ah. Very well, thank you. I thought it was some way of getting an absolute date within a given calendar system. – ArtOfWarfare Jul 14 '13 at 19:09
  • Issue with your update is that it will consider going from 11 PM on one day to 1 AM to be zero days, but I want that to return 1. It should count midnights, as Apple's example code does. When it's 6 AM Wednesday, then 6 PM Monday is 2 days ago as far as most people are concerned, but the code in your update would return just 1 day (and 12 hours, if it was set to include hours in its response.) But you can't just take days and add hours. 2 hours may or may not be 1 day, depending on when those 2 hours fall. – ArtOfWarfare Jul 14 '13 at 20:28
  • 1
    If you want to “count midnights”, then set the hour:minute:second in the `NSDateComponents` to 12:00:00 (noon, not midnight) for both the starting and ending dates, convert to `NSDate` objects, and use `components:fromDate:toDate:options:`. – rob mayoff Jul 14 '13 at 21:25
  • 1
    See Listing 14 in [“Determining Temporal Differences” in the *Date and Time Programming Guide*](http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW8). – rob mayoff Jul 14 '13 at 21:28
  • Excellent, thank you, you've answered my every question now within this topic. – ArtOfWarfare Jul 14 '13 at 22:27