iOS Convert Seconds into hours,minutes and seconds
Hi all,
In building iPhone applications you may have come across situations that you count some time in seconds, but wish to let the user view it in a more readable format such as 1 hour 38 minutes and 44 seconds. Here is a piece of code that I wrote if you need to get this done in your code.
In building iPhone applications you may have come across situations that you count some time in seconds, but wish to let the user view it in a more readable format such as 1 hour 38 minutes and 44 seconds. Here is a piece of code that I wrote if you need to get this done in your code.
- (NSString *)timeFormatted:(int)totalSeconds
{
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
NSString *timeString =@"";
NSString *formatString=@"";
if(hours > 0){
formatString=hours==1?@"%d hour":@"%d hours";
timeString = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,hours]];
}
if(minutes > 0 || hours > 0 ){
formatString=minutes==1?@" %d minute":@" %d minutes";
timeString = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,minutes]];
}
if(seconds > 0 || hours > 0 || minutes > 0){
formatString=seconds==1?@" %d second":@" %d seconds";
timeString = [timeString stringByAppendingString:[NSString stringWithFormat:formatString,seconds]];
}
return timeString;
}
This code checks to see if the number of hours, minutes or seconds is equal to one and displays either hour/hours, minute/minutes or second/seconds accordingly.
Have fun coding, guys & gals !!!
Comments
python Training in Pune
python Training in Chennai
python Training in Bangalore
Best Devops Training in pune
Devops Training in Bangalore
Power bi training in Chennai
Data Science Training in Indira nagar
Data Science training in marathahalli
Data Science Interview questions and answers
Data Science training in btm layout
Data Science Training in BTM Layout
Data science training in bangalore
data science certification in bhilai