S.Verma
S.Verma

Reputation: 199

How can I convert current time to a float value?

I'm making a slider which can be used as a countdown timer, how can I set the value of rangeCircularSlider.startPointValue to my current time value, because this takes in as a float value. I'm writing this code:

    let date=Date()
    let calendar=Calendar.current
    let hour=calendar.component(.hour, from: date)
    let minute=calendar.component(.minute, from: date)

    let dayInSeconds = 24 * 60 * 60
    rangeCircularSlider.maximumValue = CGFloat(dayInSeconds)

    rangeCircularSlider.startPointValue = CGFloat(1 * 60)
    rangeCircularSlider.endPointValue = CGFloat(8 * 60)

Upvotes: 1

Views: 1581

Answers (2)

Gi0R
Gi0R

Reputation: 1457

You can convert the date to a TimeInterval type (which is a double) and use that as starting point, for example:

let startPointValue = CGFloat(Date().timeIntervalSince1970)

Upvotes: 2

Md. Ibrahim Hassan
Md. Ibrahim Hassan

Reputation: 5467

The code gives the time elapsed since the start of third millennium.

 let now = Date()
 now.timeIntervalSinceReferenceDate

Playground Output:

"Mar 27, 2017, 10:35 AM"
512283940.76486

Refer to this link

Upvotes: 1

Related Questions