iOS時間格式化“剛剛、幾分鐘前、幾小時前”等,[包括時間戳&格式化後的時間]...
前言:
iOS中把時間轉化成“剛剛、幾分鐘前、幾小時前、幾天前、某月某日幾點幾分、.......”格式
這就需要看後臺返回什麼樣的型別了,在專案中碰到的無非就是,格式化後和時間戳了。所以,這次把這兩種我都結合起來,放到這裡,直接拿去用!
一、返回格式化後的時間 2016-10-11 12:33:33
pragma mark 時間格式轉化
注意:資料返回型別為格式化後的時間 eg: "2016-10-11 12:33:33"
-
(NSString *) compareCurrentTime:(NSString *)str
{//把字串轉為NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeDate = [dateFormatter dateFromString:str];//得到與當前時間差
NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
timeInterval = -timeInterval;
//標準時間和北京時間差8個小時
// timeInterval = timeInterval - 86060;
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小時前",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;
}
二、返回的是時間戳
pragma mark 時間格式轉化
注意:後臺返回的時間戳包括10位或者有小數點。
eg:“1480064761” 1480064761.000000
-
(NSString *)distanceTimeWithBeforeTime:(double)beTime
{
NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
double distanceTime = now - beTime;
NSString * distanceStr;NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"HH:mm"];
NSString * timeStr = [df stringFromDate:beDate];[df setDateFormat:@"dd"];
NSString * nowDay = [df stringFromDate:[NSDate date]];
NSString * lastDay = [df stringFromDate:beDate];if (distanceTime < 60) {
distanceStr = @"剛剛";
}
else if (distanceTime <6060) {
distanceStr = [NSString stringWithFormat:@"%ld分鐘前",(long)distanceTime/60];
}
else if(distanceTime <246060 && [nowDay integerValue] == [lastDay integerValue]){
distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
}
else if(distanceTime<2460602 && [nowDay integerValue] != [lastDay integerValue]){if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) { distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr]; } else{ [df setDateFormat:@"MM-dd HH:mm"]; distanceStr = [df stringFromDate:beDate]; }
}
else if(distanceTime <246060*365){
[df setDateFormat:@"MM-dd HH:mm"];
distanceStr = [df stringFromDate:beDate];
}
else{
[df setDateFormat:@"yyyy-MM-dd HH:mm"];
distanceStr = [df stringFromDate:beDate];
}
return distanceStr;
}
三、捎帶福利、獲取當前時間戳方法,返回10位。
pragma mark 獲取當前時間戳
- (NSString *)getCurrentTime{
NSDate *senddata = [NSDate date];
NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[senddata timeIntervalSince1970]];
return date2;
}
相關文章
- Pbootcms將日期時間轉換成"剛剛、幾分鐘、幾小時前"的形式boot
- asp.net 格式化顯示時間為幾個月,幾天前,幾小時前,幾分鐘前,或幾秒前ASP.NET
- 《Vue系列》timeago.js將時間戳轉換成“幾天前”“幾分鐘前”等格式VueGoJS時間戳
- 分享一段PHP格式化時間戳的程式碼,可以把時間戳轉化成幾天前,幾個月前的格式PHP時間戳
- 獲取時間戳,幾個時間點的時間戳時間戳
- 格式化時間 戳
- c++ 獲取當前時間周初凌晨時間戳(獲取當前時間週一凌晨時間戳)C++時間戳
- 如何讓時間戳的15分鐘前使用BigQuery ?時間戳
- Java取當前時間的一分鐘後,並格式化輸出Java
- js時間物件:獲取當前時間(格式化)- 程式碼篇JS物件
- 易優實現釋出時間顯示類似幾小時前、幾天前的效果
- 直播軟體搭建,當前時間、既定時間後的時間及時間比較大小
- MySQL時間戳、時間MySql時間戳
- spark sql在當前的時間戳下增加8個小時SparkSQL時間戳
- C++資料格式化4 - 格式化時間戳C++時間戳
- JavaScript 獲取指定時間前幾天日期JavaScript
- Python格式化時間Python
- 日期時間格式化
- java獲取日期差以及幾天前和幾天後的時間Java
- 查詢時間從前7天到當前時間
- 兩個時間戳的時間差時間戳
- C++資料格式化3 - 格式化時間區間(使用時長)C++
- C# 時間戳轉時間C#時間戳
- 時間型別和時間戳型別時間戳
- mysql時間操作(時間差和時間戳和時間字串的互轉)MySql時間戳字串
- JavaScript時間日期格式化JavaScript
- js時間格式化工具JS
- 格式化釋出時間
- 如何用Java獲取當前時間戳?Java時間戳
- 時間轉化,多少分鐘前,多少秒前
- 【時間戳轉普通時間格式的方法】時間戳
- android短視訊開發,Java程式碼獲取當前時間的時間戳AndroidJava時間戳
- Excel中時間戳轉換時間Excel時間戳
- 時間戳轉化為時間格式時間戳
- JS自動生成24小時時間區間,時間跨度為60或30分鐘JS
- 細說PHP筆記08(第12章)--日期和時間,建立時間戳,mktime轉換unix時間戳,獲取字串時間,獲得日期和時間資訊,日期和時間格式化輸出,microtime()獲取微秒數PHP筆記時間戳字串
- sql server 計算兩個時間 相差的 幾天幾時幾分幾秒SQLServer
- 時間戳與時間字串的多時區轉換時間戳字串
- 查詢時若時間為空,開始時間取今天的零點,結束時間取當前時間