日時オブジェクトから年月日や時分秒、曜日をとりだしたい
NSDate *date = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps; // 年月日をとりだす comps = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; NSInteger year = [comps year]; NSInteger month = [comps month]; NSInteger day = [comps day]; NSLog(@"year: %d month: %d, day: %d", year, month, day); //=> year: 2010 month: 5, day: 22 // 時分秒をとりだす comps = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date]; NSInteger hour = [comps hour]; NSInteger minute = [comps minute]; NSInteger second = [comps second]; NSLog(@"hour: %d minute: %d second: %d", hour, minute, second); //=> hour: 18 minute: 24 second: 31 // 週や曜日などをとりだす comps = [calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit) fromDate:date]; NSInteger week = [comps week]; // 今年に入って何週目か NSInteger weekday = [comps weekday]; // 曜日 NSInteger weekdayOrdinal = [comps weekdayOrdinal]; // 今月の第何曜日か NSLog(@"week: %d weekday: %d weekday ordinal: %d", week, weekday, weekdayOrdinal); //=> week: 21 weekday: 7(日曜日) weekday ordinal: 4(第4日曜日)
月日、時分秒を指定して日時オブジェクトを生成したい
// 年月日、時分秒を指定して日時オブジェクトを生成する NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:2010]; [comps setMonth:1]; [comps setDay:2]; [comps setHour:3]; [comps setMinute:4]; [comps setSecond:5]; NSDate *date = [calendar dateFromComponents:comps]; NSLog(@"date: %@", date); // => date: 2010-01-02 03:04:05 +0900
ふたつの日時の差を求めたい(年月日)
// ふたつの日時の差を求める(年月日のみ) NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps1 = [[NSDateComponents alloc] init]; [comps1 setYear:2009]; [comps1 setMonth:3]; [comps1 setDay:10]; NSDate *date1 = [calendar dateFromComponents:comps1]; NSDateComponents *comps2 = [[NSDateComponents alloc] init]; [comps2 setYear:2010]; [comps2 setMonth:5]; [comps2 setDay:20]; NSDate *date2 = [calendar dateFromComponents:comps2]; NSDateComponents *diff = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date1 toDate:date2 options:0]; NSLog(@"Year: %d Month: %d Day: %d", [diff year], [diff month], [diff day]); // => Year: 1 Month: 2 Day: 10
ふたつの日時の差を求めたい(日時分)
// ふたつの日時の差をもとめる(日時分のみ) NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps1 = [[NSDateComponents alloc] init]; [comps1 setYear:2010]; [comps1 setMonth:4]; [comps1 setDay:5]; [comps1 setHour:10]; [comps1 setMinute:30]; NSDate *date1 = [calendar dateFromComponents:comps1]; NSDateComponents *comps2 = [[NSDateComponents alloc] init]; [comps2 setYear:2010]; [comps2 setMonth:4]; [comps2 setDay:6]; [comps2 setHour:12]; [comps2 setMinute:45]; NSDate *date2 = [calendar dateFromComponents:comps2]; NSDateComponents *diff = [calendar components:(NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:date1 toDate:date2 options:0]; NSLog(@"Day: %d Hour: %d Minute: %d", [diff day], [diff hour], [diff minute]); // => Day: 1 Hour: 2 Minute: 15
n日後の日時を求めたい
// n日後の日時をもとめる NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDateComponents *sevenDays = [[NSDateComponents alloc] init]; [sevenDays setDay:7]; NSDate *sevenDaysLater = [calendar dateByAddingComponents:sevenDays toDate:now options:0]; NSLog(@"seven days later: %@", sevenDaysLater); // => seven days later: 2010-04-11 22:45:55
日時をフォーマット出力したい
// 日時をフォーマット出力する // 書式はここ http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns NSDate *now = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd(E) HH:mm:ss"]; NSString *result = [formatter stringFromDate:now]; NSLog(@"Now: %@", result);
文字列から日時を生成したい
// 文字列から日時を生成する NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate *date = [formatter dateFromString:@"2010-7-8 13:14"]; NSLog(@"Date: %@", date);
ミリ秒単位でふたつの日時の差異を求めたい
// ミリ秒単位のふたつの日時の差異を求めたい NSDate *now = [NSDate date]; NSLog(@"now: %@", now); for (int i=0; i<10; i++) { NSLog(@"delay!!"); } NSDate *then = [NSDate date]; NSLog(@"then: %@", then); NSLog(@"then - now: %1.3fsec", [then timeIntervalSinceDate:now]);
現在の日時を取得したい
NSDateのクラスメソッドdateを呼ぶと現在の日時をもつNSDateのインスタンスが返されます。
NSDate *now = [NSDate date]; NSLog(@"Now: %@", now); // => Now: 2010-08-12 18:21:39 +0900