Date style as timer, but use days

I was wondering if there was any way to get the dd:hh:mm:ss style when formatting a date as timer? Currently the following code only does hh:mm:ss - Text("\(Date(timeIntervalSince1970: 1686330135), style: .timer)")...

Any help appreciated!

Is it Swift or SwiftUI ?

I don't know of a direct way to do this.

But you coud recompute the text: Compute the time

  • from the date
let start = Date(timeIntervalSince1970: 1686330135)

compute the number of days since today

    let fromNowToStart =  1686330135 - Date().timeIntervalSince1970
    var daysCount : Int {
        Int(fromNowToStart / 86400)  // 364
    }

Then change Text with

    Text("\(daysCount) \(Date(timeIntervalSince1970: TimeInterval(1686330135 - daysCount*86400)), style: .timer)")

You will have to fine tune, to take into account when timer crosses the day limit… Then recompute the number of days and restart the timer (otherwise it reverses counting).

Could get ideas here: https://stackoverflow.com/questions/63149626/swiftui-timer-doesnt-stop-at-0

Date style as timer, but use days
 
 
Q