关于两个时间差的计算

计算一个时间距离现在的时间过了多长的时间,单位为天,小时、分钟、秒数

以天为单位计算

// 计算相差了几天

- (int)intervalSinceNow: (NSString *) theDate

{

NSDateFormatter *date=[[NSDateFormatter alloc] init];

[date setDateFormat:@"yyyy-MM-dd HH:mm zzz"];

NSDate *d=[date dateFromString:theDate];

NSTimeInterval late=[d timeIntervalSince1970]*1;

NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: dat];

NSDate *localDate = [dat  dateByAddingTimeInterval:interval];

NSTimeInterval now=[localDate timeIntervalSince1970]*1;

NSString *timeString=@"";

NSTimeInterval cha=now-late;

if (cha/86400>1)

{

timeString = [NSString stringWithFormat:@"%f", cha/86400];

timeString = [timeString substringToIndex:timeString.length-7];

return [timeString intValue];

}

return -1;

}


时间和字符串互换转换:

+ (NSString *)stringFromDate:(NSDate *)date{

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: date];

NSDate *localDate = [date  dateByAddingTimeInterval:interval];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm zzz"];

//    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];

NSString *destDateString = [dateFormatter stringFromDate:localDate];

return destDateString;

};


为了避免其中时间区域造成的误差范围尽可能的缩小采用一个时区类别记录

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: date];

NSDate *localDate = [date  dateByAddingTimeInterval:interval];

每次处理的Date都是时区格式化之后的时间,以便于计算误差减小。


两个时间之差

- (NSString *)intervalFromLastDate: (NSString *) dateString1  toTheDate:(NSString *) dateString2

{

NSArray *timeArray1=[dateString1 componentsSeparatedByString:@"."];

dateString1=[timeArray1 objectAtIndex:0];

NSArray *timeArray2=[dateString2 componentsSeparatedByString:@"."];

dateString2=[timeArray2 objectAtIndex:0];

NSLog(@"%@.....%@",dateString1,dateString2);

NSDateFormatter *date=[[NSDateFormatter alloc] init];

[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *d1=[date dateFromString:dateString1];

NSTimeInterval late1=[d1 timeIntervalSince1970]*1;

NSDate *d2=[date dateFromString:dateString2];

NSTimeInterval late2=[d2 timeIntervalSince1970]*1;

NSTimeInterval cha=late2-late1;

NSString *timeString=@"";

NSString *house=@"";

NSString *min=@"";

NSString *sen=@"";

sen = [NSString stringWithFormat:@"%d", (int)cha%60];

//        min = [min substringToIndex:min.length-7];

//    秒

sen=[NSString stringWithFormat:@"%@", sen];

min = [NSString stringWithFormat:@"%d", (int)cha/60%60];

//        min = [min substringToIndex:min.length-7];

//    分

min=[NSString stringWithFormat:@"%@", min];

//    小时

house = [NSString stringWithFormat:@"%d", (int)cha/3600];

//        house = [house substringToIndex:house.length-7];

house=[NSString stringWithFormat:@"%@", house];

timeString=[NSString stringWithFormat:@"%@:%@:%@",house,min,sen];

return timeString;

}


一个时间距现在的时间

- (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=late-now;

if (cha/3600<1) { // 分钟

timeString = [NSString stringWithFormat:@"%f", cha/60];

timeString = [timeString substringToIndex:timeString.length-7];

timeString=[NSString stringWithFormat:@"剩余%@分", timeString];

}

if (cha/3600>1&&cha/86400<1) {  // 小时

timeString = [NSString stringWithFormat:@"%f", cha/3600];

timeString = [timeString substringToIndex:timeString.length-7];

timeString=[NSString stringWithFormat:@"剩余%@小时", timeString];

}

if (cha/86400>1) // 天

{

timeString = [NSString stringWithFormat:@"%f", cha/86400];

timeString = [timeString substringToIndex:timeString.length-7];

timeString=[NSString stringWithFormat:@"剩余%@天", timeString];

}

return timeString;

}

你可能感兴趣的:(关于两个时间差的计算)