计算两个时间差的两个函数
两个时间之差
- (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 *[email protected]"";
NSString *[email protected]"";
NSString *[email protected]"";
NSString *[email protected]"";
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];
[date release];
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 *[email protected]"";
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];
}
[date release];
return timeString;
}