Objective-cで指定した日付が何曜日か取得する方法を調べたのでメモです。
初投稿ですのでお手柔らかに・・・
曜日取得
NSCalendar *calendar = [NSCalendar currentCalendar];
//2015年2月1日を指定
NSDateComponents* components = [[NSDateComponents alloc] init];
components.year = 2015;
components.month = 2;
components.day = 1;
//Date型で日付を取得
NSDate* date = [calendar dateFromComponents:components];
//指定した日付が何曜日か取得(1=日曜日〜7=土曜日)
components = [calendar components:NSWeekdayCalendarUnit fromDate:date];
//配列で曜日の名称をセット
NSArray *weekList = @[@"日曜",@"月曜",@"火曜",@"水曜",@"木曜",@"金曜",@"土曜"];
//配列から曜日番号を使って曜日名称を取得
NSLog(@"%@",[weekList objectAtIndex:[components weekday]-1]);
取得した曜日番号は日曜始まりになってるので、1=日曜日〜7=土曜日となります。
※曜日番号が1始まりなので−1しています。
いじょーです!