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 Challenge: Formatting Time

How to convert seconds

Can somebody break down how you convert the seconds into hours, minutes and seconds... I divided the seconds by sixty than divide that number by sixty to get the hours(664) but I'm lost on how I convert the rest can somebody please help me out

total seconds is 2390724

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! So you have a total number of seconds. There are 60 seconds in a minute so dividing the seconds by 60 will give you the number of minutes. There are 60 minutes in an hour so dividing the minutes by 60 will give you the number of hours. There are 24 hours in one day, so dividing the hours by 24 will give you the number of days.

Hope this helps! :sparkles:

Formatting Time

You have been given a time value measured in seconds. Your task is to define a string literal that describes the time in hours, minutes and seconds. To achieve this, you will need to convert from the value in seconds to hours, minutes and seconds. An example of the final result we're looking for:

9700 seconds is 2 hours, 41 minutes and 40 seconds */

let time = 2390724

let hours = time/3600 (664hours) let minutes = (time % 3600)/60 (5 minutes) let seconds = time % 60 (24 seconds) "(time) seconds is (hours) hours,(minutes) minutes and (seconds) seconds"

I have a few questions that I need clarification on

  1. why do we put (time % 3600) in quotations? and why do we divide the that by 60? I thought we multiple that (I'm lost lol)
  2. why do we do time % 60?
  3. I don't understand how they wrote this string. please explain it to me
    PLEASE HELP!!

Thank you for your help. Can you help me understand this paart now? Please!!

The code provided below defines the location of a tower and an enemy on a two dimensional coordinate map. towerX and towerY are the X and Y positions of the Tower while enemyX and enemyY are the X and Y values of the enemy. */

let towerX = 2.0 let towerY = 13.0

let enemyX = 4.0 let enemyY = 9.0

/*: Write code to determine how far they are from one another using the Distance Formula. The Distance Formula can be broken down into the following steps

  • Subtract the distance between the X coordinates of the Tower and Enemy and square it
  • Subtract the distance between the Y coordinates of the Tower and Enemy and square it
  • Add the two square values
  • Obtain the square root of the resulting value */

// Enter your code here let distancex = enemyX - towerX

//: For the final part, obtaining the square root, replace the 1 inside of the sqrt(1) with the value you want to compute the square root for.

import Foundation

let root = sqrt(1)

Michael Hulet
Michael Hulet
47,913 Points

Please make a separate question. Asking multiple questions in one thread makes it hard for others having similar problems to find a solution

Nicolás Melgarejo
Nicolás Melgarejo
14,945 Points
// Formatting Time excercise

let time: Int = 3234324
let horas: Int = time/3600
let minutos: Int = (time%3600)/60
let segundos: Int = time - (horas*3600 + minutos*60)

var output: String = "\(time) segudos es igual a \(horas) horas, \(minutos) minutos y \(segundos) segundos."

Formatting Time

You have been given a time value measured in seconds. Your task is to define a string literal that describes the time in hours, minutes and seconds. To achieve this, you will need to convert from the value in seconds to hours, minutes and seconds. An example of the final result we're looking for:

9700 seconds is 2 hours, 41 minutes and 40 seconds */

let time = 2390724

let hours = time/3600 (664hours) let minutes = (time % 3600)/60 (5 minutes) let seconds = time % 60 (24 seconds) "(time) seconds is (hours) hours,(minutes) minutes and (seconds) seconds"

I have a few questions that I need clarification on

  1. why do we put (time % 3600) in quotations? and why do we divide the that by 60? I thought we multiple that (I'm lost lol)
  2. why do we do time % 60?
  3. I don't understand how they wrote this string. please explain it to me
    PLEASE HELP!!
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Let's see if I can answer this point by point.

  1. I cannot see anywhere in your text above where (time%3600) appears in quotations. Did you mean parentheses? Some people respond better to sports analogies so let's try that. Assuming a football team has 21 points on the scoreboard (and they got all the extra points), we can safely assume they got 3 touchdowns right?. points/points_per_touchdown = number_of_touchdown. We divide the total points by the number of points in a touchdown. If we multiplied, as per your suggestion, we'd have 21 times 7 which would result in 147 touchdowns to score just 21 points. That's not correct :smiley: In this case, we divide the total number of seconds by the number of seconds in an hour. There are 3600 seconds in one hour. I feel like a review of basic math and algebra might be appropriate.

  2. This is the modulus operator. This will give us the remainder after dividing by 60. In this case, the total number of seconds modulus 60 will return 24. This will give us the number that cannot be evenly divided by 60. It is what is left over.

  3. I feel like you might be missing some backslashes here. I'm not sure if that's because it's missing in your code or they were omitted from your post.

"\(time) seconds is \(hours) hours,\(minutes) minutes and \(seconds) seconds"

This is called string interpolation. Wherever you see the backslash and parentheses it's going to take the value of the variable and insert it there. So where it says \(time), it will instead show 2390724. Where it says \(hours) it will instead show 664 etc.

Hope this helps! :sparkles: