//获取 字符串
1.
NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *strDate = [outputFormatter dateFromString:image.beginTime];
//修正8小时的差时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: strDate];
NSDate *endDate = [strDate dateByAddingTimeInterval: interval];
NSString *lastTime = [self compareDate:endDate];
//给字符串所在的控件赋值,一般在setmodel中赋值显示
self.timelabel.text = [NSString stringWithFormat:@"%@开播",lastTime];
//调用方法compareDate方法
-(NSString *)compareDate:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
//修正8小时之差
NSDate *date1 = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date1];
NSDate *localeDate = [date1 dateByAddingTimeInterval: interval];
//NSLog(@"nowdate=%@ olddate = %@",localeDate,date);
NSDate *today = localeDate;
NSDate *yesterday,*beforeOfYesterday;
//今年
NSString *toYears;
toYears = [[today description] substringToIndex:4];
yesterday = [today dateByAddingTimeInterval: secondsPerDay];
beforeOfYesterday = [yesterday dateByAddingTimeInterval: secondsPerDay];
NSString *todayString = [[today description] substringToIndex:10];
NSString *yesterdayString = [[yesterday description] substringToIndex:10];
NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];
NSString *dateString = [[date description] substringToIndex:10];
NSString *dateYears = [[date description] substringToIndex:4];
NSString *dateContent;
if ([dateYears isEqualToString:toYears]) {//同一年
//今 昨 前天的时间
NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
//其他时间
NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];
if ([dateString isEqualToString:todayString]){
dateContent = [NSString stringWithFormat:@"今天 %@",time];
return dateContent;
} else if ([dateString isEqualToString:yesterdayString]){
dateContent = [NSString stringWithFormat:@"明天 %@",time];
return dateContent;
}else if ([dateString isEqualToString:beforeOfYesterdayString]){
dateContent = [NSString stringWithFormat:@"前天 %@",time];
return dateContent;
}else{
return time2;
}
}else{
return dateString;
}
}