GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

time conversion - Day 19

Forums > 100 Days of SwiftUI

Hi, Recently I started again on the 100 days of swiftUI. Never finished it before but want to try it again (hopefully I will make it to day 100 this time ;)).
I'm currently finished (I think) with day 19 but do wonder if things can be done better. I searched of course online and on this forum but "time conversion" give a lot of results and it doesn't look like what I think I need.
Last time I did day 19 I also choose for the seconds/minutes/hours/days converter. Mainly because I understand the math behind that so it is way easier to see if my code is doing what is should be doing. Last time, if I for example entered 22 hours and wanted to convert that to days, it would say 0 days. So it only worked with whole numbers. (I don't know why I kept it with that, maybe because my brain thougt that Paul set to not make it to complicated, so I just continuted). This time I want to do better. So if a user for example enters 1234567 seconds and want to know how many days that is, it will say 14 days, 6 hours, 56 minutes and 7 seconds. I have that working but do wonder if it can be better. A small example; If the user selected they want to convert their given input to days, It will do this:

else if timeOutSelected == "days" {
            timeCalc = timeIn / 60 / 60 / 24
            hoursReturn = HourCalc
            minutReturn = minutCalc
            secondsReturn = secondsCalc

            returnString = "\(timeCalc) days \(hoursReturn)\(minutReturn)\(secondsReturn) "
        }

In that code runs for example hourCalc. That looks like this:

    var hourCalc: String {
        var hour = 0
        var hourReturn = ""
        let timeIn = inputToSeconds
        hour = (timeIn % 86400) / 3600   

        if hour == 0 {
            hourReturn = "0 hours "
        } else {
            hourReturn = "\(hour) hours "
        }
        return hourReturn
    }

minutReturn and secondsReturn look similar but with ofcourse different math. I'm doing the math in multiple locations and that makes me wonder if I'm doing the right thing. And even more, is their a better way in doing this?

   

You can use the DateComponentsFormatter to get an answer as a String:

import Foundation  //or UIKit or AppKit

let secs = 1234567.0

let fmt = DateComponentsFormatter()
fmt.unitsStyle = .full
fmt.allowedUnits = [.day, .hour, .minute, .second]

let fmtTime = fmt.string(from: secs)
print(fmtTime!)

Returns:

14 days, 6 hours, 56 minutes, 7 seconds

   

Working with dates and times is hard. Like, really hard – way harder than you think.

So @twoStraws made a video explaining the DateComponents code that @roosterBoy posted above.

See -> Dates are Hard

   

@Obelix @roosterboy Thankyou both! I will look into the tips this weekend.

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.