16

i try to convert a value like "898.171813964844" into 00:17:02 (hh:mm:ss).

How can this be done in objective c?

Thanks for help!

phx
  • 5,927
  • 4
  • 24
  • 24
  • You could use simple arithmatic, but I am not seeing how 898.171813964844 would be 00:17:02. What does the float refer to? – Xetius Aug 11 '09 at 08:26
  • Hey, in ruby i simple do Time.at(time - 3600).strftime("%H:%M:%S") and get the correct result. (time is the float value) – phx Aug 11 '09 at 08:29

4 Answers4

31

Final solution:

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]);
jantimon
  • 36,840
  • 23
  • 122
  • 185
phx
  • 5,927
  • 4
  • 24
  • 24
  • Why do I get something like 8 hours 1 minutes and 34 seconds when I have the value 94.000 in my interval? Am I doing something wrong? – Ben Mar 05 '10 at 15:59
  • @Ben - this is likely because your timezone is +8 hours. Try setting the timezone to GMT `[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];` – So Over It Jun 23 '12 at 04:46
  • This solution is not the best. I just moved from Slovakia to Buenos Aires and even I used the trick mention by @Ben I still get wrong conversion. Generally this works in all cases when your "Settings/Date&Time/Set Automatically" is off in iPhone. – Vanya Feb 10 '14 at 13:13
9

Assuming you are just interested in hours, minutes and seconds and that the input value is less or equal 86400 you could do something like this:

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];   
Volker Voecking
  • 5,203
  • 2
  • 38
  • 35
3

I know the answer has already been accepted, but here is my response using NSDateFormatter and taking into account timezone (to your timezone hours [eg. GMT+4] being unexpectedly added @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]]);

[side note] @phx: assuming 898.171813964844 is in seconds, this would represent 00:14:58 not 00:17:02.

So Over It
  • 3,668
  • 3
  • 35
  • 46
1
  1. Convert your NSNumber value to a NSTimeInterval with -doubleValue
  2. Convert your NSTimeInterval value to a NSDate with +dateWithTimeIntervalSinceNow:
  3. Convert your NSDate to a NSString with -descriptionWithCalendarFormat:timeZone:locale:
mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • NSTimeInterval interval = [time doubleValue]; NSDate *date = [NSDate date]; date = [NSDate dateWithTimeIntervalSinceNow:interval]; NSString *value = [date descriptionWithCalendarFormat:@"%I:%M:%S" timeZone:[NSTimeZone localTimeZone] locale:nil]; like this? But i get: warning NSDate may not respond to -descriptionWithCalendarFormat.... – phx Aug 11 '09 at 09:02
  • The non-deprecated way (or iPhone SDK way) of converting an NSDate to a string is to use an NSDateFormatter. – Wevah Aug 11 '09 at 11:03
  • Also, are you trying to convert the number into an absolute time (i.e., is it a timestamp?) or units (i.e., is the number just an amount of seconds?)? – Wevah Aug 11 '09 at 11:05
  • Nevermind, I missed your comment up above. Use an NSDateFormatter. – Wevah Aug 11 '09 at 11:06
  • -descriptionWithCalendarFormat... is not available on iPhone. Use an NSDateFormatter instead, as suggested by Wevah. – mouviciel Aug 11 '09 at 12:13