根据服务器返回的时间来格式化显示时间

+ (NSString*)intervalSinceNow: (NSString*) theDate;

{

NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];

[dateFormattersetDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.000+08:00'"];

NSTimeIntervaltimePublished = [[dateFormatterdateFromString:theDate]timeIntervalSince1970];

NSTimeIntervaltime1970 = [[NSDatedate]timeIntervalSince1970];

doubletimeSub = time1970 - timePublished;

if(timeSub <0) {

returnnil;

}

if(timeSub <60) {

return@"刚刚";

}

if(timeSub <60*60) {

return[NSStringstringWithFormat:@"%d分前", (int)timeSub /60];

}

if(timeSub <60*60*24) {

return[NSStringstringWithFormat:@"%d小时前", (int)timeSub / (60*60)];

}

if(timeSub <60*60*24*7) {

return[NSStringstringWithFormat:@"%d天前", (int)timeSub / (60*60*24)];

}

if(timeSub <60*60*24*30) {

return[NSStringstringWithFormat:@"%d周前", (int)timeSub / (60*60*24*7)];

}

if(timeSub <60*60*24*365) {

return[NSStringstringWithFormat:@"%d月前", (int)timeSub / (60*60*24*30)];

}

return[NSStringstringWithFormat:@"%d年前", (int)timeSub / (60*60*24*365)];

}

你可能感兴趣的:(根据服务器返回的时间来格式化显示时间)