You can use the NSDate
and NSCalendar
classes:
NSDate *today = [NSDate date]; //Get a date object for today's date
NSCalendar *c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
today
is an NSDate
object representing the current date; this can be used to work out the number of days in the current month. An NSCalendar
object is then instantiated, which can be used, in conjunction with the NSDate
for the current date, to return the number of days in the current month using the rangeOfUnit:inUnit:forDate:
function.
days.length
will contain the number of days in the current month.
Here are the links to the docs for NSDate
and NSCalendar
if you want more information.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…