Martin
Martin

Reputation: 2914

CountdownTimer is not working as intended (wrong time)

I'm using CountdownTimer in my app to display remaining time until specific Date. But Date is only 2 hours from current time, but if I convert millisUntilFinished to hours, it says 9 hours. Date is in UTC format.

remainingTimer = object : CountDownTimer(dateTime.time, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                remTime = millisUntilFinished
                notifyChanged(PAYLOAD_UPDATE_REM_TIME)
            }

            override fun onFinish() {
                swapTimers()
            }

        }.start()

val hours = ((remTime / (1000 * 60 * 60)).rem(24))

Upvotes: 0

Views: 271

Answers (1)

Neha Rathore
Neha Rathore

Reputation: 427

Here you have to specify timer time for how long (2 hour from now = 2*60*60*1000 millis) you want to run if you convert date in millis its not gonna work the way you want as it return date in millis,

remainingTimer = object : CountDownTimer(2*60*60*1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    remTime = millisUntilFinished
                    notifyChanged(PAYLOAD_UPDATE_REM_TIME)
                }

                override fun onFinish() {
                    swapTimers()
                }

            }.start()

    val hours = ((remTime / (1000 * 60 * 60)).rem(24))

Upvotes: 0

Related Questions