时间转化可以看看这篇文章:http://blog.csdn.net/fengsh998/article/details/9731617
加8个时区
- (NSDate *) addEightTimeZone:(NSDate *)date { NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date dateByAddingTimeInterval: interval]; return localeDate; }
当多余8个时区时,此时可以用 timeValue = (timeValue -8*60*60*1000); 减去8个时区的时间,就是现在的准确时间了。
- (NSString *)convertStringToTime:(NSString *)timeString { NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate*timeDate = [formatter dateFromString:timeString]; // timeDate = [self addEightTimeZone:timeDate]; long long timeStamp = [timeDate timeIntervalSince1970]; NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; // dat = [self addEightTimeZone:dat]; NSTimeInterval currentStamp=[dat timeIntervalSince1970]; long long val = (currentStamp - timeStamp); NSString *_timeStr; if (val > 2*24*60*60) //48以上 月日 { _timeStr = [NSString stringWithFormat:@"%@",[self ConvertTimeStampToTimeStr:timeStamp Format:@"MM月dd日"]]; } else if ((val < 2*24*60*60) && (val > 24*60*60)) //48以内 昨天 { _timeStr = @"昨天"; } else if ((val < 24*60*60) && (val > 60*60)) //24小时以内 { _timeStr = [NSString stringWithFormat:@"%@小时前",[self ConvertTimeStampToTimeStr:val Format:@"HH"]]; } else if ((val < 60*60) && (val > 60)) //1小时以内 { _timeStr = [NSString stringWithFormat:@"%@分钟前",[self ConvertTimeStampToTimeStr:val Format:@"mm"]]; } else if (val < 60) //1分钟以内 { _timeStr = @"1分钟前"; } return _timeStr; } - (NSString *)ConvertTimeStampToTimeStr:(long long )timeValue Format:(NSString*)dateFormat { timeValue = (timeValue - 8*60*60); NSDate *d = [[NSDate alloc]initWithTimeIntervalSince1970:timeValue]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:dateFormat]; //d = [self addEightTimeZone:d]; NSString*timeString=[formatter stringFromDate:d]; if ([timeString integerValue] <10) { timeString = [NSString stringWithFormat:@"%ld",(long)[timeString integerValue]]; } return timeString; }