2

I would like to make a function that input a NSDate and output the last date of the month. Do you guys know how to do it in Cocoa?

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
justicepenny
  • 2,004
  • 2
  • 24
  • 48

2 Answers2

9

It's the day before the first day of the next month. Add a month, set the day to 1, subtract a day. For setting the day to 1, you'll find it easier to go via NSCalendar. See here for details.

Community
  • 1
  • 1
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 1
    +1 The universal answer. Always right. Always works. Language, platform and framework agnostic. – S.Lott Dec 03 '10 at 11:24
2

If you're doing date computations, you should always use NSCalendar and related classes, because that's the only way to be forward-compatible with changes to calendars, support for non-"standard" calendars, and so on.

Read the Date and Time Programming Guide section on Calendrical Calculations to get an idea of how these classes work together.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102