3

次の経過時間をユーザーに表示したいと思います。

例えば、

Scanned 13 seconds ago
Scanned 2 minutes ago
Scanned 1 hour ago
Scanned 2 days ago
Scanned 1 month ago
Scanned 1 year ago

これを実現するために次の方法を使用しますが、期待どおりに機能していません。たとえば、何かをスキャンしたばかりの場合、次のように出力されます: Scanned 9 seconds 16 hours ago、これは明らかに正しくありません。それはちょうどあるべきです: Scanned 9 seconds ago

 + (NSString *)elapsedTime:(NSDate *)date {
    NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |  NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] components:desiredComponents
                                                                         fromDate:date
                                                                           toDate:[NSDate date]
                                                                          options:0];

    NSString *stringFormat = nil;
    stringFormat = [NSString stringWithFormat:@"%@", [elapsedTimeUnits year] > 0 ? ([NSString stringWithFormat: @"%d year%@ ", [elapsedTimeUnits year], [elapsedTimeUnits year] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits month] > 0 ? ([NSString stringWithFormat: @"%d month%@ ", [elapsedTimeUnits month], [elapsedTimeUnits month] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits week] > 0 ? ([NSString stringWithFormat: @"%d week%@ ", [elapsedTimeUnits week], [elapsedTimeUnits week] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits day] > 0 ? ([NSString stringWithFormat: @"%d day%@ ", [elapsedTimeUnits day], [elapsedTimeUnits day] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits hour] > 0 ? ([NSString stringWithFormat: @"%d hour%@ ", [elapsedTimeUnits hour], [elapsedTimeUnits hour] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits minute] > 0 ? ([NSString stringWithFormat: @"%d minute%@ ", [elapsedTimeUnits minute], [elapsedTimeUnits minute] == 1 ? @"" : @"s"]) : @""];
    stringFormat = [NSString stringWithFormat:@"%@%@", stringFormat, [elapsedTimeUnits second] > 0 ? ([NSString stringWithFormat: @"%d second%@ ", [elapsedTimeUnits second], [elapsedTimeUnits second] == 1 ? @"" : @"s"]) : @""];

    return [NSString stringWithFormat:@"Scanned %@ago", stringFormat];
}
4

1 に答える 1

4

各部分を確認してから追加する必要があります

stringFormat = [NSString stringWithFormat: @"%d", [elapsedTimeUnits year] > 0 ? ([NSString stringWithFormat: @"%d year%@", [elapsedTimeUnits year], [elapsedTimeUnits year] == 1 ? @"" : "s"]) : @""];

stringFormat = [NSString stringWithFormat: @"%@%@", stringFormat, [elapsedTimeUnits month] > 0 ? ([NSString stringWithFormat: @"%d month%@", [elapsedTimeUnits month], [elapsedTimeUnits month] == 1 ? @"" : "s"]) : @""];

単位を適切に変更してさらに追加し、最後にstringFormatに表示に必要な文字列が含まれます。

このコードは、 2年前にスキャンまたは5秒前にスキャンを表示します

NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit 
            | NSMinuteCalendarUnit | NSSecondCalendarUnit;

    NSDate *date = [formatter dateFromString: @"8 02 2011 14:22:59"];

    NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] components: desiredComponents
                                                                 fromDate: date
                                                                   toDate: [NSDate date]
                                                                  options:0];
    // format to be used to generate string to display
    NSString *scannedFormat = @"Scanned %d %@ ago";
    NSInteger number = 0;
    NSString *unit;

    if ([elapsedTimeUnits year] > 0) {
        number = [elapsedTimeUnits year];
        unit = [NSString stringWithFormat:@"year"];
    }
    else if ([elapsedTimeUnits month] > 0) {
        number = [elapsedTimeUnits month];
        unit = [NSString stringWithFormat:@"month"];
    }
    else if ([elapsedTimeUnits week] > 0) {
        number = [elapsedTimeUnits week];
        unit = [NSString stringWithFormat:@"week"];
    }
    else if ([elapsedTimeUnits day] > 0) {
        number = [elapsedTimeUnits day];
        unit = [NSString stringWithFormat:@"day"];
    }
    else if ([elapsedTimeUnits hour] > 0) {
        number = [elapsedTimeUnits hour];
        unit = [NSString stringWithFormat:@"hour"];
    }
    else if ([elapsedTimeUnits minute] > 0) {
        number = [elapsedTimeUnits minute];
        unit = [NSString stringWithFormat:@"minute"];
    }
    else if ([elapsedTimeUnits second] > 0) {
        number = [elapsedTimeUnits second];
        unit = [NSString stringWithFormat:@"second"];
    }
    // check if unit number is greater then append s at the end
    if (number > 1) {
        unit = [NSString stringWithFormat:@"%@s", unit];
    }
    // resultant string required
    NSString *scannedString = [NSString stringWithFormat:scannedFormat, number, unit];
于 2011-02-05T11:41:45.513 に答える