iOS計(jì)算上次日期距離現(xiàn)在多久的代碼
本文實(shí)例為大家分享了iOS上次日期距離現(xiàn)在多久的計(jì)算代碼,供大家參考,具體內(nèi)容如下
/**
* 計(jì)算上次日期距離現(xiàn)在多久
*
* @param lastTime 上次日期(需要和格式對應(yīng))
* @param format1 上次日期格式
* @param currentTime 最近日期(需要和格式對應(yīng))
* @param format2 最近日期格式
*
* @return xx分鐘前、xx小時前、xx天前
*/
+ (NSString *)timeIntervalFromLastTime:(NSString *)lastTime
lastTimeFormat:(NSString *)format1
ToCurrentTime:(NSString *)currentTime
currentTimeFormat:(NSString *)format2{
//上次時間
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
dateFormatter1.dateFormat = format1;
NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];
//當(dāng)前時間
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.dateFormat = format2;
NSDate *currentDate = [dateFormatter2 dateFromString:currentTime];
return [Utilities timeIntervalFromLastTime:lastDate ToCurrentTime:currentDate];
}
+ (NSString *)timeIntervalFromLastTime:(NSDate *)lastTime ToCurrentTime:(NSDate *)currentTime{
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
//上次時間
NSDate *lastDate = [lastTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:lastTime]];
//當(dāng)前時間
NSDate *currentDate = [currentTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:currentTime]];
//時間間隔
NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];
//秒、分、小時、天、月、年
NSInteger minutes = intevalTime / 60;
NSInteger hours = intevalTime / 60 / 60;
NSInteger day = intevalTime / 60 / 60 / 24;
NSInteger month = intevalTime / 60 / 60 / 24 / 30;
NSInteger yers = intevalTime / 60 / 60 / 24 / 365;
if (minutes <= 10) {
return @"剛剛";
}else if (minutes < 60){
return [NSString stringWithFormat: @"%ld分鐘前",(long)minutes];
}else if (hours < 24){
return [NSString stringWithFormat: @"%ld小時前",(long)hours];
}else if (day < 30){
return [NSString stringWithFormat: @"%ld天前",(long)day];
}else if (month < 12){
NSDateFormatter * df =[[NSDateFormatter alloc]init];
df.dateFormat = @"M月d日";
NSString * time = [df stringFromDate:lastDate];
return time;
}else if (yers >= 1){
NSDateFormatter * df =[[NSDateFormatter alloc]init];
df.dateFormat = @"yyyy年M月d日";
NSString * time = [df stringFromDate:lastDate];
return time;
}
return @"";
}
使用如下:
NSLog(@"\n\nresult: %@", [Utilities timeIntervalFromLastTime:@"2015年12月8日 15:50"
lastTimeFormat:@"yyyy年MM月dd日 HH:mm"
ToCurrentTime:@"2015/12/08 16:12"
currentTimeFormat:@"yyyy/MM/dd HH:mm"]);
輸出結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)中runtime常用的幾種方法示例總結(jié)
Runtime也就是所謂的“運(yùn)行時”,因?yàn)槭窃谶\(yùn)行時實(shí)現(xiàn)的。下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中runtime常用的幾種方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08
iOS 中Swift仿微信添加提示小紅點(diǎn)功能(無數(shù)字)
這篇文章主要介紹了iOS 中Swift仿微信添加提示小紅點(diǎn)功能(無數(shù)字),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05
iOS常見算法以及應(yīng)用知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于iOS常見算法以及應(yīng)用知識點(diǎn)總結(jié),有興趣的朋友們學(xué)習(xí)下。2019-10-10
Swift 去除 TableView 多余的空Cell中的橫線的方法
這篇文章主要介紹了Swift 去除 TableView 多余的空Cell中的橫線的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
iOS實(shí)現(xiàn)一個可以在屏幕中自由移動的按鈕
經(jīng)常在手機(jī)上看到可以隨意移動的按鈕,正巧最近工作遇到了這個需求,索性就寫一個,下面這篇文章主要給大家介紹了利用iOS實(shí)現(xiàn)一個可以在屏幕中自由移動的按鈕的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器使用進(jìn)階
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器的使用進(jìn)階,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03

