计算一个时间距离现在的时间差的方法
计算一个时间字符串距离现在的时间间隔
#pragma mark-一个时间距现在的时间
- (NSString *)intervalSinceNow: (NSString *) theDate
{
NSArray * timeArray;
if ([theDate rangeOfString:@"T"].location != NSNotFound)
{
timeArray=[theDate componentsSeparatedByString:@"T"];
theDate=[NSString stringWithFormat:@"%@ %@",[timeArray objectAtIndex:0],[timeArray objectAtIndex:1]];
}
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;
NSTimeInterval cha=late-now;
NSString *timeString=@"";
NSString * dayStr = @"";
NSString * hourStr = @"";
NSString * minStr = @"";
if (cha <=0)
{
timeString = @"已结束";
}
if (cha/3600/24 > 1)
{
dayStr = [NSString stringWithFormat:@"%d", (int)cha/3600/24];
timeString=[NSString stringWithFormat:@"%@天", dayStr];
cha = cha-3600*24*[dayStr intValue];
}
if (cha/3600 > 1) {
hourStr = [NSString stringWithFormat:@"%d", (int)cha/3600];
timeString=[NSString stringWithFormat:@"%@%@小时", timeString,hourStr];
cha = cha-3600*[hourStr intValue];
}
if (cha/60 > 1) {
minStr = [NSString stringWithFormat:@"%d", (int)cha/60];
timeString=[NSString stringWithFormat:@"%@%@分", timeString,minStr];
cha = cha-60*[minStr intValue];
}
if (cha > 1) {
NSString * secondStr = [NSString stringWithFormat:@"%d", (int)cha];
timeString=[NSString stringWithFormat:@"%@%@秒", timeString,secondStr];
}
return timeString;
}