Convert NSNumber (double) value into time
Asked Answered
D

4

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!

Decamp answered 11/8, 2009 at 8:18 Comment(2)
You could use simple arithmatic, but I am not seeing how 898.171813964844 would be 00:17:02. What does the float refer to?Tsarism
Hey, in ruby i simple do Time.at(time - 3600).strftime("%H:%M:%S") and get the correct result. (time is the float value)Decamp
D
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]);
Decamp answered 12/8, 2009 at 17:32 Comment(3)
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?Moidore
@Moidore - this is likely because your timezone is +8 hours. Try setting the timezone to GMT [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];Womanly
This solution is not the best. I just moved from Slovakia to Buenos Aires and even I used the trick mention by @Moidore I still get wrong conversion. Generally this works in all cases when your "Settings/Date&Time/Set Automatically" is off in iPhone.Mayonnaise
J
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];   
Jitterbug answered 11/8, 2009 at 11:25 Comment(0)
W
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.

Womanly answered 23/6, 2012 at 5:10 Comment(0)
T
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:
Twill answered 11/8, 2009 at 8:36 Comment(5)
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....Decamp
The non-deprecated way (or iPhone SDK way) of converting an NSDate to a string is to use an NSDateFormatter.Betoken
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?)?Betoken
Nevermind, I missed your comment up above. Use an NSDateFormatter.Betoken
-descriptionWithCalendarFormat... is not available on iPhone. Use an NSDateFormatter instead, as suggested by Wevah.Twill

© 2022 - 2024 — McMap. All rights reserved.