2009-08-11 15 views
15

intenté convertir un valor como "898.171813964844" en 00:17:02 (hh: mm: ss).Convertir NSNumber (doble) valor en el tiempo

¿Cómo se puede hacer esto en el objetivo c?

¡Gracias por la ayuda!

+0

Puede usar aritmático simple, pero no veo cómo 898.171813964844 sería 00:17:02. ¿A qué se refiere el flotador? – Xetius

+0

Hey, en rubí i hacer Time.at (tiempo - 3600) el simple .strftime ("% H:% M:% S") y obtener el resultado correcto. (tiempo es el valor flotante) – phx

Respuesta

30

solución final:

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

¿Por qué obtengo algo así como 8 horas 1 minuto y 34 segundos cuando tengo el valor 94,000 en mi intervalo? ¿Estoy haciendo algo mal? – Ben

+0

@Ben - esto es probable porque su zona horaria es de +8 horas. Pruebe a establecer la zona horaria a GMT '[dateFormatter settimezone: [NSTimeZone timeZoneForSecondsFromGMT: 0]];' ¿Por –

+0

-3600 del tiempo? –

1
  1. Convertir el valor NSNumber a un NSTimeInterval con -doubleValue
  2. Convertir el valor NSTimeInterval a un NSDate con +dateWithTimeIntervalSinceNow:
  3. Convertir su NSDate a un NSString con -descriptionWithCalendarFormat:timeZone:locale:
+0

NSTimeInterval interval = [time doubleValue]; NSDate * date = [NSDate date]; date = [NSDate dateWithTimeIntervalSinceNow: interval]; NSString * Valor = [fecha descriptionWithCalendarFormat: @ "% I:% M:% S" timeZone: [NSTimeZone LocalTimeZone] locale: nil]; como este? Pero recibo: advertencia NSDate puede no responder a -descriptionWithCalendarFormat .... – phx

+0

La forma no desaprobada (o forma de SDK de iPhone) de convertir un NSDate en una cadena es usar un NSDateFormatter. – Wevah

+0

Además, está tratando de convertir el número en un tiempo absoluto (es decir, se trata de una marca de tiempo?) O unidades (es decir, es el número sólo una cantidad de segundos?)? – Wevah

8

Suponiendo que usted está apenas interesado en horas, minutos y segundos y que el valor de entrada sea menor o igual a 86400, podría hacer algo como esto:

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

sé la respuesta ya ha sido aceptado, pero aquí está mi respuesta utilizando NSDateFormatter y teniendo en cuenta la zona horaria (a su zona horaria horas [por ejemplo. GMT + 4] que se añade de forma inesperada @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] @phx: suponiendo 898,171813964844 está en segundos, esto representaría 00:14:58 no 00:17:02.