provo a convertire un valore come "898.171813964844" in 00:17:02 (hh: mm: ss).Converti NSNumber (doppio) valore nel tempo
Come può essere fatto nell'obiettivo c?
Grazie per l'aiuto!
provo a convertire un valore come "898.171813964844" in 00:17:02 (hh: mm: ss).Converti NSNumber (doppio) valore nel tempo
Come può essere fatto nell'obiettivo c?
Grazie per l'aiuto!
soluzione finale:
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]);
Perché ottengo qualcosa come 8 ore 1 minuto e 34 secondi quando ho il valore 94.000 nel mio intervallo? Sto facendo qualcosa di sbagliato? – Ben
@ Ben: è probabile perché il fuso orario è di +8 ore. Prova a impostare il fuso orario su GMT '[dateFormatter setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: 0]];' –
Perché -3600 da tempo? –
-doubleValue
+dateWithTimeIntervalSinceNow:
-descriptionWithCalendarFormat:timeZone:locale:
Intervallo NSTimeInterval = [time doubleValue]; NSDate * date = [data NSDate]; date = [NSDate dateWithTimeIntervalSinceNow: interval]; NSString * value = [data descriptionWithCalendarFormat: @ "% I:% M:% S" timeZone: [NSTimeZone localTimeZone] locale: nil]; piace questo? Ma ottengo: avviso NSDate potrebbe non rispondere a -descriptionWithCalendarFormat .... – phx
Il modo non deprecato (o modo iPhone SDK) di convertire un NSDate in una stringa consiste nell'utilizzare un NSDateFormatter. – Wevah
Inoltre, stai provando a convertire il numero in un tempo assoluto (vale a dire, un timestamp?) O unità (ad esempio, il numero è solo un numero di secondi?)? – Wevah
Dando per scontato che sono solo interessati a ore, minuti e secondi e che il valore di input è inferiore o uguale a 86400 si potrebbe fare qualcosa del genere:
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];
So che la risposta è già stata accettata, ma qui è la mia risposta utilizzando NSDateFormatter e tenendo conto fuso orario (alle ore di fuso orario [ad es. GMT + 4] essendo inaspettatamente aggiunto @ 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]]);
[nota laterale] @phx: supponendo 898,171813964844 è in secondi, ciò rappresenterebbe 00:14:58 non 00:17:02.
È possibile utilizzare semplici operazioni aritmetiche, ma non vedo come 898.171813964844 sarebbe 00:17:02. A cosa si riferisce il galleggiante? – Xetius
Ehi, in ruby faccio semplicemente Time.at (time - 3600) .strftime ("% H:% M:% S") e ottieni il risultato corretto. (il tempo è il valore float) – phx