分享一个工具类实现类似qq空间发布时间的事假戳转化
#import@interface NSString (YCData)
- (NSString *)format:(NSString *)string;
-(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format;
+ (NSString *)replaceUnicode:(NSString *)unicodeStr;
@end
#import "NSString+YCData.h"
@implementation NSString (YCData)
+ (NSString *)replaceUnicode:(NSString *)unicodeStr
{
NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData
mutabilityOption:NSPropertyListImmutable
format:NULL
errorDescription:NULL];
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
-(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSInteger timeS = timestamp;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
[formatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
// (@"YYYY-MM-dd hh:mm:ss")----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeS];
// NSLog(@"%zd = %@",timestamp,confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
// NSLog(@"%@",confromTimespStr);
return confromTimespStr;
}
- (NSString *)format:(NSString *)string{
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
[inputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSDate*inputDate = [inputFormatter dateFromString:string];
NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//get date str
NSString *str= [outputFormatter stringFromDate:inputDate];
//str to nsdate
NSDate *strDate = [outputFormatter dateFromString:str];
//修正8小时的差时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: strDate];
NSDate *endDate = [strDate dateByAddingTimeInterval: interval];
// NSLog(@"endDate:%@",endDate);
NSString *lastTime = [self compareDate:endDate];
return lastTime;
}
-(NSString *)compareDate:(NSDate *)date{
NSTimeInterval secondsPerDay = 24 * 60 * 60;
//修正8小时之差
NSDate *date1 = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date1];
NSDate *localeDate = [date1 dateByAddingTimeInterval: interval];
//NSLog(@"nowdate=%@\nolddate = %@",localeDate,date);
NSDate *today = localeDate;
NSDate *yesterday,*beforeOfYesterday;
//今年
NSString *toYears;
toYears = [[today description] substringToIndex:4];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];
// 10 first characters of description is the calendar date:
NSString *todayString = [[today description] substringToIndex:10];
NSString *yesterdayString = [[yesterday description] substringToIndex:10];
NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];
NSString *dateString = [[date description] substringToIndex:10];
NSString *dateYears = [[date description] substringToIndex:4];
NSString *hourdate = [[date description] substringToIndex:14];
NSString *todayhour = [[today description] substringToIndex:14];
NSString *minitedate = [[date description] substringToIndex:17];
NSString *todayminite = [[today description] substringToIndex:17];
NSInteger miniteinteval = [[[date description] substringWithRange:(NSRange){14,2}] integerValue];
NSInteger todayminiteinteval = [[[today description] substringWithRange:(NSRange){14,2}] integerValue];
if ([minitedate isEqualToString:todayminite]) {
return @"刚刚";
}else{
if ([hourdate isEqualToString:todayhour]) {
return [NSString stringWithFormat:@"%zd分钟前",todayminiteinteval-miniteinteval];
}
else{
}
}
NSString *dateContent;
if ([dateYears isEqualToString:toYears]) {//同一年
//今 昨 前天的时间
NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
//其他时间
NSString *time2 = [[date description] substringWithRange:(NSRange){5,5}];
if ([dateString isEqualToString:todayString]){
dateContent = [NSString stringWithFormat:@"%@",time];
return dateContent;
} else if ([dateString isEqualToString:yesterdayString]){
dateContent = [NSString stringWithFormat:@"昨天 %@",time];
return dateContent;
}else if ([dateString isEqualToString:beforeOfYesterdayString]){
dateContent = [NSString stringWithFormat:@"前天 %@",time];
return dateContent;
}else{
return time2;
}
}else{
return dateString;
}
}
@end
使用起来方便,在model中的get方法中:
-(NSString *)createTime{
NSInteger strin = [_createTime integerValue];return stringArr;
NSString *str = [_createTime timestampSwitchTime:strin andFormatter:@"YYYY年MM月dd日 hh时mm分ss秒"];
NSString *stringArr = [str format:str];
return stringArr;
}
有兴趣的可以做一个更好的封装.