COMPANY SERVICE STAFF BLOG NEWS CONTACT

STAFF BLOG

スタッフブログ

TECHNICAL

テクログ

2014.05.23

Objective-C | 一週間の開始と終了日を取得

テクログ

Mac,iPhone,Objective-C 全て利用歴5ヶ月の新米プログラマーが送る

自分が使って役立ったmethod集

シリーズ第10弾です。

渡された週の真ん中の日付を渡すと週の開始と終了日を持ってきます。

開始は日曜日、終了は土曜日と定義しており、1日が日曜日の場合、終了日である土曜は7日となります。

日付の扱い方がいろいろあって何がいいのかわからなくて四苦八苦した記憶があります。

これが正解かどうかは不明ですがニッチな分、他でやってないって事でよしとしましょう。

common.h

+ (NSArray *)getDaysOfCurrentWeek:(NSDate *)weekDate;

common.m

// 一週間の開始と終了日
// return NSDate array
+ (NSArray *)getDaysOfCurrentWeek:(NSDate *)weekDate
{
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    // Create the start date components
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
    
    // 週の連番 日曜が1土曜が7
    // Get the weekday component of the current date
    NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit fromDate:weekDate];
    
    /*
     Create a date components to represent the number of days to subtract from the current date.
     The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question.  (If today is Sunday, subtract 0 days.)
     */
    
    [oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
    
    NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents toDate:weekDate options:0];
    
    //+++++++++++++++++++ end week+++++++++++++++
    [oneDayAgoComponents setDay:7- ([subcomponent weekday])];
    
    NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:weekDate options:0];
    return (NSArray *)@[beginningOfWeek, endOfWeek];
}

呼び出し方

ViewController.h に修正はありません。

#import "ViewController.h"
#import "common.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
?// Do any additional setup after loading the view, typically from a nib.
    
    // 2ヶ月前
    NSDate *today = [NSDate date];
    //    NSCalendar *cal = [NSCalendar currentCalendar];
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:-2];
    NSDate *date = [cal dateByAddingComponents:comps toDate:today options:0];
    NSArray *prev     = [common getDaysOfCurrentWeek:date];
    //NSDate *startDate = [common stringToDate:prev[0]];
    //NSDate *startDate = prev[0];
    
    NSLog(@"%@", prev);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

実行結果
(

“2014-03-23 10:56:00 +0000”,

“2014-03-29 10:56:00 +0000”

)

getDaysOfCurrentWeek:(NSDate *)weekDate の戻り値が NSArrayになっていて

特殊な戻し方をしているのがミソとなっております

以前に作成した stringToDate:baseString を利用すると、日付だけの文字列に変換出来ます。

Objective-C / 日にちの文字列を 日付(NSDate)型に変換

参考にさせていただいたサイトです

■stackoverflow

Display week & day lists for everymonth when we pass the year

■Mac Developer Library

Performing Calendar Calculations

■プロフェッショナルプログラマー

NSDate, NSDateComponents でiOSの時間を制する(その2)

そのほかのObjective-C関連の記事をお探しならコチラをどうぞ

弊社から出している拙作スケジュールのアプリです。
スケジュールというかちょっとした予定を入力して、社長、同僚、家族がどこ行った??っていうときに第三者が確認するためのアプリです
是非ダウンロードして感想をお聞かせ下さい。

『Team Scheduler』 概要
▼アプリ価格: 無料
▼対応OS: iOS(iPhone/iPod Touch)、Android
▼カテゴリ: 仕事効率化(App Store)、ビジネス(Google Play)
▼対応言語: 日本語
▼推奨環境:
iOS版: iPhone4以降、iOS6. 0以降
Android版: Android OS 2. 3以降
▼ダウンロード:
App Storeからダウンロード
Google playで手に入れよう

この記事を書いた人

core-corp

入社年

出身地

業務内容

特技・趣味

テクログに関する記事一覧

TOP