不說(shuō)廢話,項(xiàng)目中有用到過(guò)這個(gè)。后臺(tái)傳過(guò)來(lái)的13位純數(shù)字時(shí)間戳(createTimeString)
實(shí)現(xiàn)代碼
- (NSString *)updateTimeForRow:(NSString *)createTimeString {
// 獲取當(dāng)前時(shí)時(shí)間戳 1466386762.345715 十位整數(shù) 6位小數(shù)
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
// 創(chuàng)建歌曲時(shí)間戳(后臺(tái)返回的時(shí)間 一般是13位數(shù)字)
NSTimeInterval createTime = [createTimeString longLongValue]/1000;
// 時(shí)間差
NSTimeInterval time = currentTime - createTime;
NSInteger sec = time/60;
if (sec<60) {
return [NSString stringWithFormat:@"%ld分鐘前",sec];
}
// 秒轉(zhuǎn)小時(shí)
NSInteger hours = time/3600;
if (hours<24) {
return [NSString stringWithFormat:@"%ld小時(shí)前",hours];
}
//秒轉(zhuǎn)天數(shù)
NSInteger days = time/3600/24;
if (days < 30) {
return [NSString stringWithFormat:@"%ld天前",days];
}
//秒轉(zhuǎn)月
NSInteger months = time/3600/24/30;
if (months < 12) {
return [NSString stringWithFormat:@"%ld月前",months];
}
//秒轉(zhuǎn)年
NSInteger years = time/3600/24/30/12;
return [NSString stringWithFormat:@"%ld年前",years];
}