iOS - 新浪微博API發(fā)布微博時(shí)間返回值與當(dāng)前時(shí)間對(duì)比,實(shí)現(xiàn)幾天前,幾小時(shí)前,幾分鐘前
這個(gè)問題在度娘上搜了好半天都沒有搜到,好不容易搜到復(fù)制代碼到項(xiàng)目中一測(cè)試就出問題了,的出來值相差太遠(yuǎn),為了解決這個(gè)問題,我是絞盡腦汁的想辦法,查問題出在哪里,浪費(fèi)了好多時(shí)間,終于找到問題所在了,原來是當(dāng)前時(shí)間在對(duì)比過去時(shí)間時(shí)出來問題。
網(wǎng)上復(fù)制的代碼:
+ (NSString *) compareCurrentTime:(NSString *)str
{
//把字符串轉(zhuǎn)為NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *timeDate = [dateFormatter dateFromString:str];
//得到與當(dāng)前時(shí)間差
NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
timeInterval = -timeInterval;
//標(biāo)準(zhǔn)時(shí)間和北京時(shí)間差8個(gè)小時(shí)
timeInterval = timeInterval - 8*60*60;
long temp = 0;
NSString *result;
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"剛剛"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%d分鐘前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%d小時(shí)前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%d天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%d月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%d年前",temp];
}
return result;
}
既然知道了問題所在,這下就好解決了!
我針對(duì)當(dāng)前時(shí)間在對(duì)比過去時(shí)間的問題,去搜了一下都有哪些對(duì)比時(shí)間的代碼。知道問題所在才能對(duì)癥下藥
錯(cuò)誤代碼:NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow] ;
這個(gè)錯(cuò)處在了[timeDate timeIntervalSinceNow]上,不可以這寫,這樣寫得出時(shí)間差的值非常大,而且還是負(fù)值
所以我就用了下面的寫法
正確代碼:NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:timeDate];
就是把當(dāng)前時(shí)間和過去時(shí)間去作對(duì)比,[date2 timeIntervalSinceDate:timeDate];這樣還是比較好的,這樣出偏差的幾率很小
本以為這樣就可以了,可是運(yùn)行后才發(fā)現(xiàn)還有問題存在,然后我就從獲取時(shí)間差下面開始找,問題是在NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:timeDate]; 這行代碼下面的兩行代碼中出錯(cuò)的
錯(cuò)誤代碼:
timeInterval = -timeInterval;
timeInterval = timeInterval - 8*60*60;
就是這兩行代碼,從這兩行代碼中打印輸出后,發(fā)現(xiàn)所得出來的值是有問題的
沒辦法又去搜了,可是,沒有得到自己想要的答案。一怒之下就把這兩行代碼給干掉了,運(yùn)行后發(fā)現(xiàn),我靠,好了
刪掉這兩行代碼是對(duì)的,得出了正確的答案了
說了這么多的廢話,我就把正確的代碼貼出來,代碼如下:
+ (NSString *)compareCurrentTime:(NSString *)str
{
//把字符串轉(zhuǎn)為NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss Z yyyy"];
dateFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
NSDate *timeDate = [dateFormatter dateFromString:str];
NSDate *date2 = [NSDate date];
//得到與當(dāng)前時(shí)間差
NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:timeDate];
long temp = 0;
NSString *result;
if (timeInterval < 60) {
result = [NSString stringWithFormat:@"剛剛"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分鐘前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小時(shí)前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
}
else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
這就是正確的代碼了,到處結(jié)束了!
希望能幫助到你!