前言:
iOS中把時(shí)間轉(zhuǎn)化成“剛剛霹娄、幾分鐘前能犯、幾小時(shí)前、幾天前犬耻、某月某日幾點(diǎn)幾分踩晶、.......”格式
這就需要看后臺(tái)返回什么樣的類型了,在項(xiàng)目中碰到的無非就是枕磁,格式化后和時(shí)間戳了渡蜻。所以,這次把這兩種我都結(jié)合起來,放到這里茸苇,直接拿去用排苍!
一、返回格式化后的時(shí)間 2016-10-11 12:33:33
pragma mark 時(shí)間格式轉(zhuǎn)化
注意:數(shù)據(jù)返回類型為格式化后的時(shí)間 eg: "2016-10-11 12:33:33"
-
(NSString *) compareCurrentTime:(NSString *)str
{//把字符串轉(zhuǎn)為NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
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 - 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小時(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;
}
二学密、返回的是時(shí)間戳
pragma mark 時(shí)間格式轉(zhuǎn)化
注意:后臺(tái)返回的時(shí)間戳包括10位或者有小數(shù)點(diǎn)淘衙。
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;
}
三、捎帶福利腻暮、獲取當(dāng)前時(shí)間戳方法彤守,返回10位。
pragma mark 獲取當(dāng)前時(shí)間戳
- (NSString *)getCurrentTime{
NSDate *senddata = [NSDate date];
NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[senddata timeIntervalSince1970]];
return date2;
}