在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中的月份名称时,是否有一种优雅的方式来获得月份?
答案 0 :(得分:0)
例如,您可以使用:
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MMM"];
NSDate *aDate = [formatter dateFromString:@"Aug"];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:aDate];
NSLog(@"Month: %i", [components month]);
或:
...
[formatter setDateFormat:@"MMMM"];
NSDate *aDate = [formatter dateFromString:@"August"];
...