//日期轉xx前 @interface NSString(date) + (NSString *)dateString:(NSString *)dateStr; @end @implementation NSString (date) + (NSString *)dateString:(NSString *)dateStr { NSDate *date = [NSDate dateFromString:dateStr withFormat:@"yyyy-MM-dd HH:mm:ss"]; if (!date) { return @""; } int minites = [[NSDate date] timeIntervalSinceDate:date]/60; if (minites < 0) { return @""; } if (minites < 60) {//不到一小時 return [NSString stringWithFormat:@"%d分鐘前",minites]; } else if(minites < 60 * 24)//不到一天 { return [NSString stringWithFormat:@"%d小時前",minites / 60]; } else if(minites < 60 * 24 * 3)//不大於三天 { return [NSString stringWithFormat:@"%d天前",minites / 60 / 24]; } //可以根據自己需要再添加幾個月前,甚至幾年前 return [date stringWithFormat:@"MM-dd”];//返回一個xx月xx日 } @end