關(guān)于兩個(gè)時(shí)間差的計(jì)算

計(jì)算一個(gè)時(shí)間距離現(xiàn)在的時(shí)間過(guò)了多長(zhǎng)的時(shí)間,單位為天,小時(shí)、分鐘、秒數(shù)

以天為單位計(jì)算

// 計(jì)算相差了幾天

- (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;

}


時(shí)間和字符串互換轉(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表示時(shí)區(qū),zzz可以刪除,這樣返回的日期字符將不包含時(shí)區(qū)信息。

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

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

NSString *destDateString = [dateFormatter stringFromDate:localDate];

return destDateString;

};


為了避免其中時(shí)間區(qū)域造成的誤差范圍盡可能的縮小采用一個(gè)時(shí)區(qū)類(lèi)別記錄

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: date];

NSDate *localDate = [date? dateByAddingTimeInterval:interval];

每次處理的Date都是時(shí)區(qū)格式化之后的時(shí)間,以便于計(jì)算誤差減小。


兩個(gè)時(shí)間之差

- (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];

//? ? 小時(shí)

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;

}


一個(gè)時(shí)間距現(xiàn)在的時(shí)間

- (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) { ?// 小時(shí)

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

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

timeString=[NSString stringWithFormat:@"剩余%@小時(shí)", timeString];

}

if (cha/86400>1) // 天

{

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

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

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

}

return timeString;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容