14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSDate 指定日以降の1週間を曜日指定で取得

Last updated at Posted at 2013-02-19

NSDateのカテゴリにしました。
NSDateが保持する日付を含むその日からの1週間を曜日指定で配列で取得出来ます。
NSDateとNSDateComponentsを行ったり来たり。
NSDateだけで取得とかもっと簡潔な方法ないのかな。

NSDate+Extras.h
typedef enum {
    YSWeekdayTypeSunday = 1 << 0,
    YSWeekdayTypeMonday = 1 << 1,
    YSWeekdayTypeTuesday = 1 << 2,
    YSWeekdayTypeWednesday = 1 << 3,
    YSWeekdayTypeThursday = 1 << 4,
    YSWeekdayTypeFriday = 1 << 5,
    YSWeekdayTypeSaturday = 1 << 6,
} YSWeekdayType;

@interface NSDate (Extras)

- (NSDateComponents*)dateAndTimeComponents;
- (NSArray*)oneWeekDateWithEnableWeekdayType:(YSWeekdayType)type;

@end
NSDate+Extras.m
@implementation NSDate (Extras)

- (NSDateComponents*)dateAndTimeComponents
{
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    return [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:self];
}

- (NSArray *)oneWeekDateWithEnableWeekdayType:(YSWeekdayType)type
{
    if (type == 0) {
        return nil;
    }
    // selfを含むその日からの1週間を取得
    NSUInteger oneWeekNum = 7;
    NSMutableDictionary *oneWeekDict = [NSMutableDictionary dictionaryWithCapacity:oneWeekNum];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    for (int i = 0; i < oneWeekNum; i++) {
        NSDateComponents *comp = [self dateAndTimeComponents];
        [comp setDay:comp.day + i];
        NSDate *newDate = [cal dateFromComponents:comp];
        NSDateComponents * newComp = [newDate dateAndTimeComponents];
        [oneWeekDict setObject:newComp forKey:[NSString stringWithFormat:@"%d", newComp.weekday]];
    }
    
    // 取得した1週間から有効な曜日のみを抜き出す
    NSMutableArray *resultArr= [NSMutableArray array];
    YSWeekdayType compType = type;
    // NSDateComponentsのweekdayの値は1〜7(日〜土)
    for (int i = 1; i <= oneWeekNum; i++) {
        if (compType % 2 == 1) {
            NSDateComponents *comp = [oneWeekDict objectForKey:[NSString stringWithFormat:@"%d", i]];
            [resultArr addObject:[cal dateFromComponents:comp]];
        }
        compType >>= 1;
    }
    return resultArr;
}

@end
呼び出し例
// 月, 火, 水を指定
NSLog(@"%@", [self.datePicker.date oneWeekDateWithEnableWeekdayType:YSWeekdayTypeMonday | YSWeekdayTypeTuesday | YSWeekdayTypeWednesday]);

/* 今日は2/19(火)で2/19(火), 2/20(水), 2/25(月)を取得
NSLog
 "2013-02-25 14:46:00 +0000",
 "2013-02-19 14:46:00 +0000",
 "2013-02-20 14:46:00 +0000"
*/
14
12
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
14
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?