計算一個時間距離現(xiàn)在的時間過了多長的時間早敬,單位為天,小時乌逐、分鐘逻住、秒數(shù)
以天為單位計算
// 計算相差了幾天
- (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;
}
時間和字符串互換轉(zhuǎn)換:
+ (NSString *)stringFromDate:(NSDate *)date{
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localDate = [date? dateByAddingTimeInterval:interval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//zzz表示時區(qū),zzz可以刪除创肥,這樣返回的日期字符將不包含時區(qū)信息达舒。
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm zzz"];
//? ? [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
NSString *destDateString = [dateFormatter stringFromDate:localDate];
return destDateString;
};
為了避免其中時間區(qū)域造成的誤差范圍盡可能的縮小采用一個時區(qū)類別記錄
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localDate = [date? dateByAddingTimeInterval:interval];
每次處理的Date都是時區(qū)格式化之后的時間叹侄,以便于計算誤差減小巩搏。
兩個時間之差
- (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;
}
一個時間距現(xiàn)在的時間
- (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;
}