Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 25
    The last value (seconds % 3600) % 60 can be optimized to seconds % 60. No need to extract the hours first. Commented Nov 7, 2014 at 7:02
  • @GoZoner - i can't seem to be getting the printSecondsToHoursMinutesSeconds function to be working properly. This is what I have in a playground, but printSecondsToHoursMinutesSeconds isnt returning anything: import UIKit func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) { return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60) } let (h,m,s) = secondsToHoursMinutesSeconds(27005) func printSecondsToHoursMinutesSeconds (seconds:Int) -> () { let (h, m, s) = secondsToHoursMinutesSeconds (seconds) println ("(h) Hours, (m) Minutes, (s) Seconds") } Commented Nov 10, 2014 at 2:26
  • printSecondstoHoursMinutesSeconds() doesn't return anything (see the -> () return type in the function declaration). The function prints something; which does not show up in a Playground. If you want it to return something, say a String then eliminate the println() call and fix the function's return type. Commented Nov 10, 2014 at 20:58
  • @GoZoner - just a quick question in your syntax above. How would I declare that 27005 in a variable? I'm working on a tool right now to get my feet wet with swift. I have a constant that displays the amount of seconds (generated after my basic calculations). let cocSeconds = cocMinutes * 60. (cocSeconds is what I want to use in place of 27005.) I think the problem I am having is cocSeconds is a double, and in your syntax, you're using Ints. How would I go about adjusting this code to put a variable in place of 27005? Thank you so much in advance!!! Commented Nov 29, 2014 at 3:56
  • The solution I gave works for Int types because of the nature of the / and % functions. That is / drops the factional part. The same code won't work for Double. Specifically 2 == 13/5 but 2.6 == 13.0/5. Therefore you would need a different implementation for Double. I've updated my answer with a note on this. Commented Nov 29, 2014 at 5:04