标签:
- (NSString*)intervalSinceNow:(NSString*)theDate
{
NSArray* timeArray = [theDate componentsSeparatedByString:@"."];
theDate = [timeArray objectAtIndex:0];
NSDateFormatter* date = [[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* d = [date dateFromString:theDate];
NSTimeInterval late = [d timeIntervalSince1970] * 1;
NSDate* dat = [NSDate date];
NSTimeInterval now = [dat timeIntervalSince1970] * 1;
NSString* timeString = @"";
NSTimeInterval cha = now - late;
if (cha / 3600 < 1) {
timeString = [NSString stringWithFormat:@"%f", cha / 60];
timeString = [timeString substringToIndex:timeString.length - 7];
timeString = [NSString stringWithFormat:@"%@分", timeString];
}
else if (cha / 86400 < 1) {
timeString = [NSString stringWithFormat:@"%f", cha / 3600];
timeString = [timeString substringToIndex:timeString.length - 7];
timeString = [NSString stringWithFormat:@"%@小时", timeString];
}
else if (cha / (86400 * 30) < 1) {
timeString = [NSString stringWithFormat:@"%f", cha / 86400];
timeString = [timeString substringToIndex:timeString.length - 7];
timeString = [NSString stringWithFormat:@" %@天", timeString];
}
else if (cha / (86400 * 30 * 12) < 1) {
timeString = [NSString stringWithFormat:@"%f", cha / (86400 * 30)];
timeString = [timeString substringToIndex:timeString.length - 7];
timeString = [NSString stringWithFormat:@" %@月", timeString];
}
else {
timeString = [NSString stringWithFormat:@"%f", cha / (86400 * 30 * 12)];
timeString = [timeString substringToIndex:timeString.length - 7];
timeString = [NSString stringWithFormat:@" %@年", timeString];
}
return timeString;
}
iOS一个时间距现在的时间方法 intervalSinceNow NSDateFormatter
标签:
原文地址:http://www.cnblogs.com/yangdahong/p/5801966.html