顯示發(fā)送時間(幾分鐘前,幾小時前,幾天前)

- (NSString*) compareCurrentTime:(NSString*)str

{

? ? //把字符串轉(zhuǎn)為NSdate

? ? //? ? NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

? ? //? ? [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

? ? //? ? currDate = [formatter stringFromDate:[NSDate date]];

? ? long long time = [str longLongValue];

? ? NSDate *d = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];

? ? NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

? ? [dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm"];

? ? NSString*currentDateString = [dateFormatterstringFromDate:d];

? ? NSDate*currentDate = [NSDatedate];

? ? NSTimeIntervaltimeInterval = [currentDatetimeIntervalSinceDate:d];

? ? longtemp =0;

? ? NSString*result;

? ? if(timeInterval/60<1)

? ? {

? ? ? ? result = [NSString stringWithFormat:@"剛剛"];

? ? }

? ? elseif((temp = timeInterval/60) <60){

? ? ? ? result = [NSStringstringWithFormat:@"%ld分鐘前",temp];

? ? }

? ? elseif((temp = temp/60) <24){

? ? ? ? result = [NSStringstringWithFormat:@"%ld小時前",temp];

? ? }

? ? elseif((temp = temp/24) <30){

? ? ? result = [NSString stringWithFormat:@"%ld天前",temp];

?? ?}

? ? elseif((temp = temp/30) <12){

? ? ? ? result = [NSString stringWithFormat:@"%ld月前",temp]; ? ?}

? ? else{

? ? ? ? temp = temp/12;

? ? ? ? result = [NSString stringWithFormat:@"%ld年前",temp];

? ? }

? ? return? result;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容