使用objective-c获取任何月份的所有日子 [英] Get all days of any month with objective-c
本文介绍了使用objective-c获取任何月份的所有日子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个看似简单的问题......我怎样才能返回任何指定月份的天数列表?
A seemingly simple question...how can I return a list of days for any specified month?
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];
我基本上想要使用它,但今天更换
比如说,一月份,所以我可以返回所有那些日子
I basically want to use that, but replace today
with say, the month of January, so I can return all of those days
推荐答案
Carl的答案适用于Mac。以下适用于Mac或iPhone(没有 dateWithNaturalLanguageString:
可用)。
Carl's answer works on Mac. The following works on Mac or iPhone (no dateWithNaturalLanguageString:
available there).
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
// Set your year and month here
[components setYear:2015];
[components setMonth:1];
NSDate *date = [calendar dateFromComponents:components];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSLog(@"%d", (int)range.length);
这篇关于使用objective-c获取任何月份的所有日子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文