2

我正在使用该NSDate-TimeAgo 来格式化我的 iOS 应用程序中的相对时间。它在其单元格标题中显示每个帖子的相对时间,如下所示:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *myDate = [df dateFromString: self.datePostedString];
        UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(230,0,80,50)];
        DDLogVerbose(@"Output is: \"%@\"", [myDate dateTimeAgo]);
        time.text = [myDate dateTimeAgo];
        time.textAlignment = NSTextAlignmentRight;
        [time setFont:[UIFont systemFontOfSize:10]];

但是,这个库给了我我觉得太长的字符串。最后一分钟的帖子将被标记为"a minute ago"。我希望它更短,例如"1m".

是否可以在不必修改库的情况下做到这一点,或者是否有替代方案已经这样做了?

4

5 回答 5

3

我认为您可能应该编写自己的函数。

- (NSString *)timeAgo:(NSDate *)compareDate{
  NSTimeInterval timeInterval = -[compareDate timeIntervalSinceNow];
  int temp = 0;
  NSString *result;
  if (timeInterval < 60) {
    result = [NSString stringWithFormat:@"Just now"];   //less than a minute
  }else if((temp = timeInterval/60) <60){
    result = [NSString stringWithFormat:@"%dm",temp];   //minutes ago
  }else if((temp = temp/60) <24){
    result = [NSString stringWithFormat:@"%dh",temp];   //hours ago
  }else{
    temp = temp / 24;
    result = [NSString stringWithFormat:@"%dd",temp];   //days ago
  }
  return  result;
}
于 2013-12-27T03:05:39.450 回答
2

这是一个基于 Lei Wang 答案的 Swift 实现。

func getShortRelativeDateFor(date: NSDate) ->String { 

    let timeInterval = -date.timeIntervalSinceNow

    switch timeInterval {
    case 0..<60:
            return String(format: "%.fs", timeInterval)
    case 60..<(60 * 60):
        return String(format: "%.fm", timeInterval / 60)
    case (60 * 60)..<(60 * 60 * 24):
        return String(format: "%.fh", timeInterval / (60 * 60))
    case (60 * 60 * 24)..<(60 * 60 * 24 * 365):
        return String(format: "%.fd", timeInterval / (60 * 60 * 24))
    default:
        return String(format: "%.fy", timeInterval / (60 * 60 * 24 * 365))
    }
}
于 2015-04-21T13:27:47.717 回答
1

如果不更改第三方类,您将无法实现这一点。在您的情况下,NSDate-TimeAgo 库对您来说太强大了。你甚至不需要任何“以前”的结果。所以,我个人建议你在自己的代码上转换时间字符串,比如(只是一个例子,可能这里有一些性能问题,你可以使用 Time Profile 来测试它):

+ (NSString *)stringForDisplayFromDate:(NSDate *)date
{
    if (!date) {
        return nil;
    }

    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(kCFCalendarUnitYear
                                                     |kCFCalendarUnitMonth
                                                     |kCFCalendarUnitWeek
                                                     |kCFCalendarUnitDay
                                                     |kCFCalendarUnitHour
                                                     |kCFCalendarUnitMinute)
                                           fromDate:date
                                             toDate:currentDate
                                            options:0];
    if (components.year == 0) {
        // same year
        if (components.month == 0) {
            // same month
            if (components.week == 0) {
                // same week
                if (components.day == 0) {
                    // same day
                    if (components.hour == 0) {
                        // same hour
                        if (components.minute < 10) {
                            // in 10 mins
                            return @"now";
                        } else {
                            // 10 mins age
                            return [NSString stringWithFormat:@"%dm", (int)(components.minute/10)*10];
                        }
                    } else {
                        // different hour
                        return [NSString stringWithFormat:@"%dh", components.hour];
                    }
                } else {
                    // different day
                    return [NSString stringWithFormat:@"%dd", components.day];
                }    
            } else {
                // different week
                return [NSString stringWithFormat:@"%dW", components.week];
            }
        } else {
            // different month
            return [NSString stringWithFormat:@"%dM", components.month];
        }
    } else {
        // different year
        return [NSString stringWithFormat:@"%dY", components.year];
    }

    return [self stringForDisplayFromDate:date prefixed:NO];
}
于 2013-12-27T02:57:52.457 回答
1

您应该使用DateTools。请参阅README 中的 Time Ago 部分,它完全符合您的要求:

NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4];
NSLog(@"Time Ago: %@", timeAgoDate.shortTimeAgoSinceNow);

//Output:
//Time Ago: 4s

请注意,此解决方案支持 33 种语言(在撰写本文时)。

于 2014-12-19T06:43:25.013 回答
0

此函数将返回从 sec 到 years 的 NSString。就像您的日期是“1 秒前”或“1 分钟前”或“1 年前”等等......它会同样返回......

-(NSString*)HourCalculation:(NSString*)PostDate

{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormat setTimeZone:gmt];
    NSDate *ExpDate = [dateFormat dateFromString:PostDate];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
    NSString *time;
    if(components.year!=0)
    {
        if(components.year==1)
        {
            time=[NSString stringWithFormat:@"%ld year",(long)components.year];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld years",(long)components.year];
        }
    }
    else if(components.month!=0)
    {
        if(components.month==1)
        {
            time=[NSString stringWithFormat:@"%ld month",(long)components.month];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld months",(long)components.month];
        }
    }
    else if(components.week!=0)
    {
        if(components.week==1)
        {
            time=[NSString stringWithFormat:@"%ld week",(long)components.week];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
        }
    }
    else if(components.day!=0)
    {
        if(components.day==1)
        {
            time=[NSString stringWithFormat:@"%ld day",(long)components.day];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld days",(long)components.day];
        }
    }
    else if(components.hour!=0)
    {
        if(components.hour==1)
        {
            time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
        }
    }
    else if(components.minute!=0)
    {
        if(components.minute==1)
        {
            time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
        }
    }
    else if(components.second>=0)
    {
        if(components.second==0)
        {
            time=[NSString stringWithFormat:@"1 sec"];
        }
        else
        {
            time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
        }
    }
    return [NSString stringWithFormat:@"%@ ago",time];
}
于 2014-12-19T07:04:46.117 回答