2011-02-05 13 views
3

iPhone - NSDateComponents/NSCalendarと経過時間、私は、次の経過時間ユーザに表示したい

例えば、

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

は、私が使用して(例えば、3時間前にスキャンしました。)これを達成するための方法に従っていますが、それは期待どおりには機能しません。たとえば、何かをスキャンしただけの場合、次のように出力されます。 スキャンされた9秒16時間前、明らかに正しくありません。それだけでなければなりません: スキャンされた9秒前。あなたは各部分をチェックしてから を適切するユニットを変更することで、エンドstringFormatの意志でより多くを追加

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"]) : @""]; 

を追加する必要があり

+ (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

は、表示するあなたに必要な文字列が含まれています。

このコードはまたはスキャン5秒前スキャンした2年前に、あなたのコード内の

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]; 
+0

いくつかのエラーが表示されます。あなたは完全な作業方法を持っていますか? – fuzz

+0

私は、日付コンポーネントに値がある場合、** year = 2、day = 5、minute = 10 **、何を表示したいですか?** 2年前にスキャン**または* *スキャンした2年5日10分前**? –

関連する問題