Sitemap
JSaw

JSaw

Time Calculator

2 min readDec 28, 2022

--

Project 4of 5 freeCodeCamp’s Scientific Computing with Python series

Get JSaw’s stories in your inbox

Join Medium for free to get updates from this writer.

For the background of this series, please refer to the first article here.

Press enter or click to view image in full size
Picture by Miguel Á. Padriñán

For this project, we basically need to create a function named add_time that does 3 things:

  1. Takes a starting time in 12-hour clock format, and the starting time may come with the day of the week
  2. Taking an input in the format of “hours:minutes” and add to the starting time
  3. Then provide the resulting time in 12-hour clock format + if day of week provided at starting time to provide the day of week + if the ending time is the next day or more than 1 day later, to state this fact in brackets

Converting the starting time

To make it easy to add the time, we basically need to convert the starting time to 24 hours, which you can use the if..else statement to check for the “AM” or “PM” in the starting time and add accordingly.

Breaking down the steps to create the output for the formula

  1. Adding the minutes -> if more than 60 minutes to keep the “full” hours to add to step 2
  2. Adding the hours -> add on the “full” hours at step 1 above -> if more than 24 hours, to keep the “full” days to add to step 3
  3. To save the “days later” and then using if..else statement to decide if we want to use the “next day” or “x days later”
  4. Convert the time to 12-hour clock format
  5. Another if..else to set the day of the week if day of the week is provided by user in input. Quick and easy is to store the days of the week in a python list and count accordingly (tip : there are 7 days in a week, and step 3 above would have the number of days later to add)
  6. Put the output of all the steps above in one nice sentence to be outputted by the function.

The above mostly is using the if..else statement, then we can use the .append .join .strip to clean the final output.

Hope the above helps!

--

--