Convert NSNumber (double) value into time
28,837
Solution 1
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]);
Solution 2
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];
Solution 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.
Solution 4
- Convert your NSNumber value to a NSTimeInterval with
-doubleValue
- Convert your NSTimeInterval value to a NSDate with
+dateWithTimeIntervalSinceNow:
- Convert your NSDate to a NSString with
-descriptionWithCalendarFormat:timeZone:locale:
Author by
phx
Updated on June 23, 2020Comments
-
phx almost 4 years
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!
-
Chris Hillery almost 15 yearsThe non-deprecated way (or iPhone SDK way) of converting an NSDate to a string is to use an NSDateFormatter.
-
Chris Hillery almost 15 yearsAlso, 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?)?
-
Chris Hillery almost 15 yearsNevermind, I missed your comment up above. Use an NSDateFormatter.
-
mouviciel almost 15 years-descriptionWithCalendarFormat... is not available on iPhone. Use an NSDateFormatter instead, as suggested by Wevah.
-
Ben about 14 yearsWhy 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?
-
So Over It almost 12 years@Ben - this is likely because your timezone is +8 hours. Try setting the timezone to GMT
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
-
Vanya over 10 yearsThis 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.