2009-08-11 8 views
15

"898.171813964844"のような値を00:17:02(hh:mm:ss)に変換しようとしています。NSNumber(double)の値を時刻に変換する

どのようにこれを客観的なcで行うことができますか?

ありがとうございました!

+0

単純な算術を使うことができますが、私は898.171813964844が00:17:02の方法を見ていません。 floatは何を参照していますか? – Xetius

+0

ちょっと、ルビーで簡単にTime.at(時間 - 3600).strftime( "%H:%M:%S")と正しい結果を得る。 (時刻は浮動小数点値です) – phx

答えて

30

最終的な解決策:

NSNumber *time = [NSNumber numberWithDouble:([online_time doubleValue] - 3600)]; 
NSTimeInterval interval = [time doubleValue];  
NSDate *online = [NSDate date]; 
online = [NSDate dateWithTimeIntervalSince1970:interval];  
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter setDateFormat:@"HH:mm:ss"]; 

NSLog(@"result: %@", [dateFormatter stringFromDate:online]); 
+0

私の時間間隔に94.000という値がある場合、なぜ8時間1分34秒というようなものが得られますか?私は何か間違っているのですか? – Ben

+0

@Ben - あなたのタイムゾーンが+8時間である可能性があります。タイムゾーンをGMT '[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]に設定してみてください。 ' –

+0

時間からの理由-3600? –

1
  1. あなたのNSNumber値は、あなたのNSTimeInterval値はあなたがちょうど興味を持っていると仮定すると、-descriptionWithCalendarFormat:timeZone:locale:
+0

NSTimeInterval interval = [time doubleValue]; NSDate * date = [NSDate date]; date = [NSDate dateWithTimeIntervalSinceNow:interval];NSString *値= [日付descriptionWithCalendarFormat:@ "%I:%M:%S" timeZone:[NSTimeZone localTimeZone]ロケール:なし]; このような?しかし、私は得る: 警告NSDateが-descriptionWithCalendarFormatに応答しない可能性があります.... – phx

+0

NSDateを文字列に変換する非推奨の方法(またはiPhone SDKの方法)は、NSDateFormatterを使用することです。 – Wevah

+0

また、数字を絶対時間(つまりタイムスタンプですか)または単位(つまり秒数ですか)に変換しようとしていますか? – Wevah

8

でNSStringのにあなたのNSDateを変換+dateWithTimeIntervalSinceNow:

  • でNSDateに変換-doubleValue
  • でNSTimeIntervalに変換時間、分、秒、入力値が86400以下であることを確認します。

    NSNumber *theDouble = [NSNumber numberWithDouble:898.171813964844]; 
    
    int inputSeconds = [theDouble intValue]; 
    int hours = inputSeconds/3600; 
    int minutes = (inputSeconds - hours * 3600)/60; 
    int seconds = inputSeconds - hours * 3600 - minutes * 60; 
    
    NSString *theTime = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds]; 
    
  • 3

    私は答えがすでに受け入れたが、ここに私の応答NSDateFormatterを使用して、タイムゾーンの時間にアカウントのタイムゾーン([例に取っているされています知っています。 GMT + 4]予想外に追加さ@Ben)

    NSTimeInterval intervalValue = 898.171813964844; 
        NSDateFormatter *hmsFormatter = [[NSDateFormatter alloc] init]; 
        [hmsFormatter setDateFormat:@"HH:mm:ss"]; 
        [hmsFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
        NSLog(@"formatted date: %@", [hmsFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:intervalValue]]); 
    

    [サイドノート] @phx:898.171813964844秒であると仮定すると、これは、午後12時14分58秒ない夜12時17分02秒に相当するであろう。

    関連する問題