4
4

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.

指定した日付は第何週なのか調べてみる

Last updated at Posted at 2015-01-31

指定した日付が第何週目にあたるのか調べてみました。
用途としては「2015-02-02T00:00:00+00:00」とJSONで複数日付が渡され、
それぞれが第何週目のグループに分けられればよいのかを調べるために使います。

まずString型で日付が渡ってくるのでNSDate型にする必要があります。
Objective-cやり始めたころに作ったメソッドがあったのでそれを流用します。

NSString→NSDate
-(NSDate*)remakeDate:(NSString*)day{
    day = [day stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterFullStyle];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];
    NSDate *remakeDay = [dateFormatter dateFromString:day];
    if(!remakeDay){
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mmZZZZ"];
        remakeDay = [dateFormatter dateFromString:day];
    }
    if(!remakeDay){
        [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
        remakeDay = [dateFormatter dateFromString:day];
    }
    return remakeDay;
}

第何週目か確認したい日付のリストはテスト用に以下のように準備しておきます。

NSArray *dayList = [NSArray arrayWithObjects:
                    [self remakeDate:@"2015-02-02T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-05T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-12T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-14T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-16T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-17T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-18T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-20T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-21T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-22T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-26T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-27T00:00:00+00:00"],
                    [self remakeDate:@"2015-02-28T00:00:00+00:00"],
                    nil];

上記のリストをログに出力しながら確認してみます。


NSDateComponents* components = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekOfMonthCalendarUnit;
for (int i = 0; i<[dayList count]; i++) {
    components = [calendar components:flags fromDate:[dayList objectAtIndex:i]];
    NSLog(@"%ld年%ld月%ld日は第%ld週目です。"   ,(long)[components year]
                                            ,(long)[components month]
                                            ,(long)[components day]
                                            ,(long)[components weekOfMonth]);
}

出力した結果は以下のようになりました。

2015年2月2日は第1週目です。
2015年2月5日は第1週目です。
2015年2月12日は第2週目です。
2015年2月14日は第2週目です。
2015年2月16日は第3週目です。
2015年2月17日は第3週目です。
2015年2月18日は第3週目です。
2015年2月20日は第3週目です。
2015年2月21日は第3週目です。
2015年2月22日は第4週目です。
2015年2月26日は第4週目です。
2015年2月27日は第4週目です。
2015年2月28日は第4週目です。

いじょーです!

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?