[ios開発]NSDateとNSCalendarを利用して具体的な時間を取得するサンプルコード
サンプルコード:
NSDate *nowDate = [NSDate date]; //現在時間
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps;
comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:nowDate];
int nowYear = [comps year];
int nowMonth = [comps month];
int nowDay = [comps day];
int nowWeek = [comps weekday];
NSString *weekStr = [[NSString alloc] init];
switch (nowWeek) {
case 1:
weekStr = @"日曜日";
break;
case 2:
weekStr = @"月曜日";
break;
case 3:
weekStr = @"火曜日";
break;
case 4:
weekStr = @"水曜日";
break;
case 5:
weekStr = @"木曜日";
break;
case 6:
weekStr = @"金曜日";
break;
case 7:
weekStr = @"土曜日";
break;
default:
break;
}
NSLog(@"年:%d;月:%d;日:%d;%@",nowYear,nowMonth,nowDay,weekStr);