I'm calculating the average travel time from one point to another by using the following calculation:
float tSpeed = [[NSString stringWithFormat:#"%f", speed] floatValue];
float duration = totDistance/tSpeed;
tripDuration_Label.text = [NSString stringWithFormat:#"%f", duration];
speed is in miles per hour so the output is in hours.
This gives me a float value for the time, I need to convert it to time (hh:mm).
Thanks
1 hour is 60 minutes so:
NSInteger hours = duration; // 2.5 -> 2
NSInteger minutes = ( duration - hours ) * 60; // ( 2.5 - 2 ) * 60
tripDuration_Label.text = [NSString stringWithFormat:#"%d:%02d", hours, minutes];
Probably you would like to do the conversion to time like in this example but before you need to get the integer values from float.
Fot this you can use modulo to sparate things before and after comma.
Related
NSNumber * uniqueId = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970])];
Milliseconds is not included in the above code.If i used like below code ,it is printing negative values.
NSNumber * uniqueId1 = [NSNumber numberWithInt:([[NSDate date] timeIntervalSince1970] * 1000)];
Can we get time stamp with milliseconds as int????
#AnjaniG you can use one of them..
NSString *strTimeStamp = [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970] * 1000];
int timestamps = [strTimeStamp intValue];
NSLog(#"number int = %d",timestamps);
You should not use an Int, [[NSDate date] timeIntervalSince1970] * 1000 is bigger than INT_MAX. You should use long long to store the value.
NSNumber * uniqueId1 = [NSNumber numberWithLongLong:([[NSDate date] timeIntervalSince1970] * 1000)];
The problem is that you are casting a floating point to an integer.
As per the documentation, timeIntervalSince1970 returns an NSTimeInterval, which is a double, not an integer.
In your first code example, you're actually discarding the milliseconds by casting.
In your second code example, you are overflowing the integer. After multiplying the value by 1000, it is too large to fit in an integer.
In the end, you're just doing too much code and you shouldn't need to really worry about this.
NSTimeInterval interval = [NSDate date].timeIntervalSince1970;
NSNumber *timestamp = #(interval * 1000.);
Here, I use the proper documented type of NSTimeInterval. Then I multiply that by 1,000 to change seconds to milliseconds. Finally, I use Clang literal syntax to instruct the compiler to create the appropriate NSNumber.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 2 DATES.
End Date
Current Date
Now, I want to find NSTimeInterval and Calculate remaining time in Days, Hours, Minutes, Seconds.
I do not want to use NSDateComponents.
I want some formula that calculate that gives remaining time in Days, Hours, Minutes, Seconds.
I tried this below formula but that formula gives remaining Hours, Minutes, Seconds.
But how do I calculate this Days, Hours, Minutes, Seconds ??
I'm using this below code
#property (nonatomic, assign) NSTimeInterval secondsLeft;
_hours = (int)self.secondsLeft / 3600;
_minutes = ((int)self.secondsLeft % 3600) / 60;
_seconds = ((int)self.secondsLeft %3600) % 60;
Some of your calculations are incorrect. You want:
_hours = (int)self.secondsLeft / 3600;
_minutes = (int)self.secondsLeft / 60 % 60;
_seconds = (int)self.secondsLeft % 60;
This assumes _hours, _minutes, and _seconds are of type int (or some other appropriate integer type).
If you want to format the NSTimeInterval into a useful string formatted properly for the user's locale, use NSDateComponentsFormatter:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
NSString *result = [formatter stringFromTimeInterval:self.secondsLeft];
I'm making a timer that takes an hourly wage input and tracks how much money is made per second. The formatting on the timer works just fine but once the pennies hits 99 it zeros out and only adds to the dollars. Here is what I've got.
seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
if(pennies > 99){
dollars++;
pennies = 0;
}
self.moneyDisplay.text = [NSString stringWithFormat: #"$%02d.%02d", dollars, pennies];
Try this:
seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
float dollars = pennies / 100.0;
self.moneyDisplay.text = [NSString stringWithFormat: #"$%.2f", dollars];
or:
seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
int cents = pennies % 100;
int dollars = pennies / 100;
self.moneyDisplay.text = [NSString stringWithFormat: #"$%02d.%02d", dollars, cents];
May be you want to do this (without if condition):
dollars = pennies / 100;
pennies = pennies % 100;
self.moneyDisplay.text = [NSString stringWithFormat: #"$%02d.%02d", dollars, pennies];
Note that: The operands of the % operator shall have integer type.
Hope this helps.. :)
I want to know how to do some simple equations in my iOS app, if anyone could point me in the right direction that would be wonderful!
I need to know how to convert an NSNumber that represents minutes to an NSString which represents hours and minutes (example: 100 = 1 hour and 40 minutes)
I also want to know if its possible to convert something like 2013-02-08T10:50:00.000 to 10:50AM
Thanks for any tips you guys might have.
For the conversion :
NSNumber *yourNumber = [NSNumber numberWithInt100];
NSInteger hour = [yourNumber intValue] / 60;
NSInteger minutes = [yourNumber intValue] % 60;
NSString *time_stamp = [NSString stringWithFormat:#"%d hour and %d minutes",hour,minutes];
As the comments suggested you can use NSDateFormatter to format your NSDate.
Now i am trying to get the length of Songs in iOS.
- (NSString *)returnofTotalLength
{
float duration = [[self.player.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue]/60.0f;
NSString *length = [NSString stringWithFormat:#"%.2f",duration];;
NSString *totalLength = [length stringByReplacingOccurrencesOfString:#"." withString:#":"];
return totalLength;
}
above codes is the total length of song that show like 5:90.
You know that 5:90 can't be true because 60 seconds is 1 minute.
It's should be 6:30.
So i want to limit that value for 1 minute (60 seconds).
How can i do it Please help me?
Thanks.
This is simple math. Pseudocode:
minutes = (int)(seconds / 60);
rest = seconds % 60;
result = minutes:rest
Objc:
int seconds = 150;
int minutes = (int)(seconds / 60);
int rest = seconds % 60;
return [NSString stringWithFormat:#"%i:%i", minutes, rest];
do following :
min=(int)duration/60;
sec=duration%60;
than append minutes and second
If your time crosses to hours then you can go for this :
NSInteger seconds = duration % 60;
NSInteger minutes = (duration / 60) % 60;
NSInteger hours = duration / (60 * 60);
NSString *result = nil;
result = [NSString stringWithFormat:#"%02ld:%02ld:%02ld", hours, minutes, seconds];
I just got answer for my own question.
Thanks for other answers. :)
NSTimeInterval currentProgress = [[self.player.nowPlayingItem valueForProperty:MPMediaItemPropertyPlaybackDuration] floatValue];
float min = floor(currentProgress/60);
float sec = round(currentProgress - min * 60);
NSString *time = [NSString stringWithFormat:#"%02d:%02d", (int)min, (int)sec];
return time;
That will return NSString with Complete Format.