【发布时间】:2014-08-15 15:51:33
【问题描述】:
在 iOS 中,我想计算一个月中由月份名称指定为 NSString 的天数。
我可以得到当月的天数如下:
-(NSUInteger) getNumberOfDaysInMonth:(NSString *)monthName
{
NSCalendar *cal = [NSCalendar ];
NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
NSUInteger numberOfDaysInMonth = rng.length;
return numberOfDaysInMonth;
}
当我知道目标 c 中的月份名称时,是否有一种优雅的方式来获取月份?
【问题讨论】:
-
查看 NSDateFormatter 并使用here 描述的模式。请注意,有很多方法可以将月份表示为字符串。考虑“September”、“Sept”、“S”、“09”、“9”、“Sep”、“10 月前的一个月”。
标签: objective-c ios7 nscalendar