• 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
Getting the last date of the month?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Getting the last date of the month?


  • Subject: Getting the last date of the month?
  • From: Nick Zitzmann <email@hidden>
  • Date: Mon, 19 Mar 2007 21:33:28 -0600

I'm currently using the following strategy to get an NSDate pointing to the last day in the month: (firstDayOfMonth is an NSDate that is pointing to the first day in the month, calendar is an NSCalendar)

dateComponents = [calendar components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:firstDayOfMonth];
[dateComponents setDay:[calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDayOfMonth].length];
lastDayOfMonth = [calendar dateFromComponents:dateComponents];


This code makes the assumption that all days in the month are contiguous. 99.9999999...% of the time that is true, but in the month of October 1582 that is not true, for reasons we recently discussed on the list (it's the month of the Julian->Gregorian calendar switch- over).

Does anyone have a better method of figuring out how many days are in a month that does not involve creating an expensive loop, and makes no assumptions like the above? If so, then what did you do? (IMPORTANT: It has to work with calendars other than the Gregorian calendar, so always using 31 for October is not an acceptable solution.)

Nick Zitzmann
<http://www.chronosnet.com/>


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: Getting the last date of the month?
      • From: Greg Titus <email@hidden>
  • Prev by Date: Re: NSDate/NSCalendarDate/NSTimezone..
  • Next by Date: Re: Background if Login Item launched
  • Previous by thread: Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
  • Next by thread: Re: Getting the last date of the month?
  • Index(es):
    • Date
    • Thread