NSDateComponentsクラスを使用すると、NSDateインスタンスの日、月、および年に直接アクセスできるようになります。
NSDate *someDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:someDate];
NSLog(@"Year: %d", [components year]);
NSLog(@"Month: %d", [components month]);
NSLog(@"Day: %d", [components day]);
ここNSDateComponents比較法だ:私はNSDateComponents比較方法をテストしていません
- (BOOL)isFirstCalendarDate:(NSDateComponents *)first beforeSecondCalendarDate:(NSDateComponents *)second
{
if ([first year] < [second year]) {
return YES;
} else if ([first year] == [second year]) {
if ([first month] < [second month]) {
return YES;
} else if ([first month] == [second month]) {
if ([first day] < [second day]) {
return YES;
}
}
}
return NO;
}
。ユニットテストの恩恵を受けるでしょう。
私は誰がそれを行うのか知っていますが、どうすれば比較できますか?だから私はObj1 - 2/5/12午後5時 Obj2 - 2/5/12午後12時..私は時間を切り捨てて、それらを一緒にグループ化するために@ "yyyy-MM-dd"形式を使用することができますか? – Jon
実際には日付のみを使用している形式を使用します。 「時間を切り捨てる」必要はありません。 – mvds