国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現時間顯示幾分鐘前,幾小時前以及剛剛的方法示例

2019-10-21 18:49:13
字體:
來源:轉載
供稿:網友

前言

本文實現的效果類似于QQ空間里的好友發表的動態,會顯示好友發表的時間,這里是處理顯示幾小時前,幾分鐘前,剛剛,昨天,前天這樣的格式,下面來一起看看吧。

一:剛剛,幾分鐘前,幾小時前

//時間NSString *createdTimeStr = @"2017-01-01 21:05:10";//把字符串轉為NSdateNSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *timeDate = [dateFormatter dateFromString:createdTimeStr];//得到與當前時間差NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];timeInterval = -timeInterval;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 = timeInterval/3600) > 1 && (temp = timeInterval/3600) <24){ result = [NSString stringWithFormat:@"%ld小時前",temp];}else{ result = createdTimeStr;} NSLog(@"%@",result);

二:剛剛,幾分鐘前,幾小時前,昨天,前天

//時間NSString *createdTimeStr = @"2017-01-01 21:05:10";//把字符串轉為NSdateNSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];NSDate *timeDate = [dateFormatter dateFromString:createdTimeStr];//得到與當前時間差NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];timeInterval = -timeInterval;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 = timeInterval/3600) > 1 && (temp = timeInterval/3600) <24){ result = [NSString stringWithFormat:@"%ld小時前",temp];}else if ((temp = timeInterval/3600) > 24 && (temp = timeInterval/3600) < 48){ result = [NSString stringWithFormat:@"昨天"];}else if ((temp = timeInterval/3600) > 48 && (temp = timeInterval/3600) < 72){ result = [NSString stringWithFormat:@"前天"];}else{ result = createdTimeStr;} NSLog(@"%@",result);

總結

以上就是這篇文字的全部內容了,希望本文的內容對各位iOS開發者能帶來一定的幫助,如果有疑問大家可以留言交流。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五莲县| 宝鸡市| 平罗县| 天镇县| 永泰县| 股票| 彭山县| 铁力市| 西充县| 灵丘县| 肃南| 武强县| 兰西县| 耒阳市| 砚山县| 靖西县| 雷波县| 乳山市| 石楼县| 罗甸县| 黑河市| 兴仁县| 山西省| 乐安县| 公安县| 华坪县| 夹江县| 西吉县| 蒙自县| 娄底市| 林周县| 桂东县| 南京市| 尉氏县| 张家港市| 永顺县| 渝中区| 建平县| 南宁市| 库尔勒市| 黄冈市|