0
function setTime(){

  const now=new Date();

//   second hand

  const seconds=now.getSeconds();

  const secondDegree=((seconds/60)*360+90);

  const secondhand=document.querySelector('.sec-hand');

  secondhand.style.transform=`rotate(${secondDegree}deg)`;

// minute hand
  
  const minhand=document.querySelector('.min-hand');

  const mins=now.getMinutes();

  const minDegree=((mins/60)*360+90);

  minhand.style.transform=`rotate(${minDegree}deg)`;

//  hour hand 

  const hourhand=document.querySelector('.hour-hand');

  const hour=now.getHours();

  const hourDegree=((hour/12)*360+90);

  hourhand.style.transform=`rotate(${hourDegree}deg)`;
}
setInterval(setTime,1000);
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
  • What do you mean with "real server time"? You know that server times also depend on the location, where they are? – JKD Jan 22 '21 at 08:33
  • hey actually my problem is when im changing my local pc time the clock is updating its time accordingly. how to get correct time even when my local pc's time is wrong – Ayush Jain Jan 22 '21 at 08:46
  • Does this answer your question? [when the time of system is wrong ,how can I get correct time in javascript?](https://stackoverflow.com/questions/53005544/when-the-time-of-system-is-wrong-how-can-i-get-correct-time-in-javascript) – JKD Jan 22 '21 at 08:57

1 Answers1

0

For what i know, javascript can't reach server time. Have been there once before, and fixed it with a xml request call to local php file which reads the current server time.

Optional you can also change timezone to desired values.

PHP File

<?php

echo time();

JS File

var now;
function getDate(){
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);
                now = new Date(xmlhttp.responseText*1000);
            }
            else if (xmlhttp.status == 400) {
                alert('There was an error 400');
            }
            else {
                alert('something else other than 200 was returned');
            }
        }
    };

    xmlhttp.open("GET", "time.php", true);
    xmlhttp.send();
}

function setTime(){

    //   second hand

    const seconds=now.getSeconds();

    const secondDegree=((seconds/60)*360+90);

    const secondhand=document.querySelector('.sec-hand');

    secondhand.style.transform=`rotate(${secondDegree}deg)`;

    // minute hand

    const minhand=document.querySelector('.min-hand');

    const mins=now.getMinutes();

    const minDegree=((mins/60)*360+90);

    minhand.style.transform=`rotate(${minDegree}deg)`;

    //  hour hand

    const hourhand=document.querySelector('.hour-hand');

    const hour=now.getHours();

    const hourDegree=((hour/12)*360+90);

    hourhand.style.transform=`rotate(${hourDegree}deg)`;

}

getDate();

setInterval(setTime,1000);
Jayr
  • 592
  • 1
  • 3
  • 14