2011-12-04 57 views

回答

0

您可以在實體類使用此功能:

-(NSString*)dateToStringForSectionTitels{ 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
NSString *formattedDateString; 
[dateFormatter locale]; 

NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDateComponents *components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]]; 
NSDate *today = [cal dateFromComponents:components]; 
components = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:self.startDate]; 
NSDate *otherDate = [cal dateFromComponents:components]; 

if([today isEqualToDate:otherDate]) { 
    formattedDateString = @"Today"; 
}else{ 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    formattedDateString = [dateFormatter stringFromDate:self.startDate]; 
} 

NSTimeInterval distanceBetweenDates = [self.startDate timeIntervalSinceDate:[NSDate date]]; 
double secondsInAnMinute = 60; 
NSInteger minutsBetweenDates = distanceBetweenDates/secondsInAnMinute; 

if (minutsBetweenDates<30) { 
    formattedDateString = @"Starting Soon"; 
}else if(minutsBetweenDates<0){ 
    formattedDateString = @"In the past"; 
} 


return formattedDateString; 

}

它將返回

  1. 日期的字符串,如果該事件是在未來
  2. 「今天」如果事件是今天。
  3. 「即將開始」,如果事件開始在30分鐘內 - 。(你可以把它的範圍,以你希望什麼都
  4. 「過去」,如果該事件是在過去

然後。剛剛通過「dateToStringForSectionTitels」你獲取結果控制器。

祝你好運。

+0

這工作,謝謝! –

相關問題