iOS中格式化的相对时间短?

时间:2013-12-27 02:50:21

标签: ios nsdate nsdateformatter relative-date

我正在使用NSDate-TimeAgo library格式化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"

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

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

这是基于王磊回答的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))
    }
}

答案 2 :(得分:1)

如果不更改第三方课程,则无法实现此目的。 在您的情况下,NSDate-TimeAgo库对您来说太强大了。你甚至不需要任何“以前”的结果。所以,我个人建议您在自己的代码上转换时间字符串,例如(只是一个例子,这里可能存在一些性能问题,您可以使用时间配置文件对其进行测试):

+ (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];
}

答案 3 :(得分:0)

您应该使用DateTools。请参阅Time Ago section in the README,它完全符合您的要求:

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

//Output:
//Time Ago: 4s

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

答案 4 :(得分:0)

此函数将从秒到年返回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];
}