Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

いじょーです!

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away