pabot99
pabot99

Reputation: 25

How to get current time in seconds and adds with player duration in Swift?

I want to get the current time in seconds and adds the seconds with player duration and other double value.

Heres the code I tried but didn't work.

let date = Date()
        let calendar = Calendar.current
        let now = calendar.component(.second, from: date)
        let start_time = now + player.duration + (myModel.diff * 60)

The code for myModel.diff

var diff: Double = 1

and when I try this code, I'm getting error saying

Binary operator '+' cannot be applied to operands of type 'Int' and 'TimeInterval' (aka 'Double')

Please help me to solve this issue

Upvotes: 0

Views: 2926

Answers (1)

jnpdx
jnpdx

Reputation: 52555

You can get your current code to compile by casting now to a Double:

let start_time = Double(now) + player.duration + (myModel.diff * 60)

However, I'm not completely sure what you mean by "current time in seconds." For example, if the current time is 08:00:03, now will be 3. At 08:01:15, it'll be 15 -- if that is your desired result, you're in good shape.

If your desired result is to get a more complete timestamp (seconds since 1970 is common, for example) of the current time, you can use something like:

Date().timeIntervalSince1970

Feel free to clarify your question and I can adjust the answer.

Upvotes: 2

Related Questions