Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Solution: Formatting Time

Dan Poynor
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dan Poynor
Data Analysis Techdegree Graduate 62,952 Points

Alternate way of getting minutes okay?

Instead of

let minutes = (time % 3600) / 60

the solution I came up with was

let minutes = time / 60 % 60

To me this reads left to right

  1. divide the time in seconds (2390724) by 60 which results in 39845 minutes
  2. then figure out how many hours are in that result: 39845 % 60 (664 hours which is 39840 minutes)
  3. then the remainder is how many minutes are left over: 39845 - 39840 = 5

Is there any benefit to solving this one way or the other? Maybe using the larger int 3600 reduces number of computing cycles figuring out how many times a number can go into another number?

Probably not very important when figuring out just one time difference but just curious.

1 Answer

I solved the problem this way. Not sure one way or another is important. I'd guess you shouldn't do calculations within your string interpolation. Which is what I took away from this exercise.

let time = 2390724

let seconds = 1
let minutes = 60
let hours = 3600

let result = "\(time) seconds is \(time / hours) hours, \((time % hours) / minutes) minutes and \((time % hours) % minutes) seconds"