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

Re: Getting the last date of the month?


  • Subject: Re: Getting the last date of the month?
  • From: Greg Titus <email@hidden>
  • Date: Tue, 20 Mar 2007 10:51:03 -0700


On Mar 19, 2007, at 8:33 PM, Nick Zitzmann wrote:

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.)

If you are starting with the first day of the month, as your "firstDayOfMonth" variable name suggests, then you ought to be able to add one month and subtract one day to get the last day of the month. Untested, written in Mail, but something like:


dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
[dateComponents setDay:-1];
lastDayOfMonth = [calendar dateByAddingComponents:dateComponents toDate:firstDayOfMonth options:0];
[dateComponents release];


Hope this helps,
	- Greg
_______________________________________________

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:
    • [SOLVED] Re: Getting the last date of the month?
      • From: Nick Zitzmann <email@hidden>
    • Re: Getting the last date of the month?
      • From: "Jim Thomason" <email@hidden>
References: 
 >Getting the last date of the month? (From: Nick Zitzmann <email@hidden>)

  • Prev by Date: Re: Detecting when menubar is visible
  • Next by Date: Re: Getting the last date of the month?
  • Previous by thread: Getting the last date of the month?
  • Next by thread: Re: Getting the last date of the month?
  • Index(es):
    • Date
    • Thread