LoginSignup
8
7

More than 5 years have passed since last update.

指定した日付が何曜日か取得してみた

Last updated at Posted at 2015-01-31

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しています。

いじょーです!

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7