L
L
lib rumay2021-09-09 17:30:40
JavaScript
lib rumay, 2021-09-09 17:30:40

Why does js countdown timer show calculated time 3 hours more? How to fix?

Hello! The problem is that the written countdown timer shows 3 hours more. Please tell me how to fix?

const deadLine = '2021-09-10';

    function getTimeRemaining(endtime) {  
        const t = Date.parse(endtime) - Date.parse(new Date()), 
              days = Math.floor(t / (1000 * 60 * 60 * 24)), 
              hours = Math.floor(t / (1000 * 60 * 60) % 24), 
              minutes = Math.floor((t / 1000 / 60) % 60),
              seconds = Math.floor((t / 1000) % 60);

        return {           
            'total': t,
            'days': days,
            'hours': hours,
            'minutes': minutes,
            'seconds': seconds
        };
    }

    function getZero(num) {
        if (num >= 0 && num < 10) {
             return `0${num}`;
        } else {
            return num;
        }
    }

    function setClock(selector, endtime ) {
        const timer = document.querySelector(selector),
              days = timer.querySelector('#days'),
              hours = timer.querySelector('#hours'),
              minutes = timer.querySelector('#minutes'),
              seconds = timer.querySelector('#seconds'),
              timeInterval = setInterval(updateClock, 1000);
        
        updateClock();

        function updateClock() { 
            const t = getTimeRemaining(endtime); 

            days.innerHTML =  getZero(t.days);
            hours.innerHTML = getZero(t.hours);
            minutes.innerHTML = getZero(t.minutes);
            seconds.innerHTML = getZero(t.seconds);

            if (t.total <= 0) {    
                clearInterval(timeInterval);
            } 
        }
    }

    setClock('.timer', deadLine);


<div class="wrapper">
        <div class="timer_wrapper">
            <div class="title">It is a timer</div>
            <div class="timer">
                <div class="timer_block">
                    <span id="days">00</span>
                    days
                </div>
                <div class="timer_block">
                    <span id="hours">00</span>
                    hours
                </div>
                <div class="timer_block">
                    <span id="minutes">00</span>
                    minutes
                </div>
                <div class="timer_block">
                    <span id="seconds">00</span>
                    seconds
                </div>
            </div>
        </div>
    </div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Steppp, 2021-09-09
@Steppp

Perhaps because the timezone is +0, not like yours +3)
getTimezoneOffset

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question