- (NSString *)transformTimeMaker:(NSString *)timestring
{
NSString *returnString;
NSDate *timeDate = [self timeFormattTansform:timestring]; //时间戳变成字符串传入
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:timeDate];
NSDate *nowDate = [[NSDate date] dateByAddingTimeInterval:interval];
//两个时间间隔
NSTimeInterval timeInterval = [timeDate timeIntervalSinceDate:nowDate];
timeInterval = -timeInterval;
long temp = 0;
if (timeInterval < 60) {
returnString = [NSString stringWithFormat:@"刚刚"];
} else if ((temp = timeInterval / 60) < 60) {
returnString = [NSString stringWithFormat:@"%ld分钟前", temp];
} else if ((temp = timeInterval / (60 * 60)) < 24) {
returnString = [NSString stringWithFormat:@"%ld小时前", temp];
} else if ((temp = timeInterval / (24 * 60 * 60)) < 30) {
returnString = [NSString stringWithFormat:@"%ld天前", temp];
} else if (((temp = timeInterval / (24 * 60 * 60 * 30))) < 12) {
returnString = [NSString stringWithFormat:@"%ld月前", temp];
} else {
temp = timeInterval / (24 * 60 * 60 * 30 * 12);
returnString = [NSString stringWithFormat:@"%ld年前", temp];
}
return returnString;
}
- (NSDate *)timeFormattTansform:(NSString *)timeString
{
NSString *timeStampString = timeString;
NSTimeInterval interval = [timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
return date;
}
网友评论