在項目中,我們可能會面對各種各樣的對於時間的需求,在這裏提供幾種可能會用到的需求代碼
1、與今天的時間做比較,返回日期差值
代碼:
- (NSInteger)compareWithToday { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *today = [NSDate date]; NSString *todayStr = [dateFormatter stringFromDate:today]; today = [dateFormatter dateFromString:todayStr]; NSInteger interval = (NSInteger) [self timeIntervalSinceDate:today]; NSInteger intervalDate = 0; if (interval <= 0) { intervalDate = interval / (24 * 60 * 60) - 1; } else { intervalDate = interval / (24 * 60 * 60); } return intervalDate; }
2、距離當前的時間間隔描述
代碼:
- (NSString *)timeIntervalDescription { NSTimeInterval timeInterval = -[self timeIntervalSinceNow]; if (timeInterval < 60) { return @"1分鐘內"; } else if (timeInterval < 3600) { return [NSString stringWithFormat:@"%.f分鐘前", timeInterval / 60]; } else if (timeInterval < 86400) { return [NSString stringWithFormat:@"%.f小時前", timeInterval / 3600]; } else if (timeInterval < 2592000) {//30天內 return [NSString stringWithFormat:@"%.f天前", timeInterval / 86400]; } else if (timeInterval < 31536000) {//30天至1年內 NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"M月d日"]; return [dateFormatter stringFromDate:self]; } else { return [NSString stringWithFormat:@"%.f年前", timeInterval / 31536000]; } }
該方法主要用於計算時間距離當前時間的時間間隔描述,主要使用時間戳進行比較。思路是先計算出時間戳與當前時間的時間戳的差值,然後進行比較。
3、分解時間
代碼:
#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal) #define CURRENT_CALENDAR [NSCalendar currentCalendar] - (NSInteger) nearestHour { NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * 30; NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; NSDateComponents *components = [CURRENT_CALENDAR components:NSCalendarUnitHour fromDate:newDate]; return components.hour; } - (NSInteger) hour { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.hour; } - (NSInteger) minute { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.minute; } - (NSInteger) seconds { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.second; } - (NSInteger) day { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.day; } - (NSInteger) month { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.month; } - (NSInteger) week { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.weekOfMonth; } - (NSInteger) weekday { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.weekday; } - (NSInteger) nthWeekday // e.g. 2nd Tuesday of the month is 2 { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.weekdayOrdinal; } - (NSInteger) year { NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self]; return components.year; }
這些方法主要是用於將時間中某一個單位下的內容以數字的形式返回。
比如 2016-08-26 08:55:48 +0000 是零時區下的當前時間,
使用 - (NSInteger) year 方法的輸出結果就是 2016;
使用 - (NSInteger) hour 方法的輸出結果就是 當前時區的時間,我們在 8時區 ,結果就是 16;
使用 - (NSInteger) seconds 方法的輸出結果就是 48 ...