Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript analogue clock code question

gifted idiot

New Coder
Hi all, first time posting here...

I have built this analogue clock (code below) but I can't get my head around how to add an additional hand that displays a 24 hour cycle. I'm pretty sure there isn't provision for both 12 hour and 24 hour hands in javascript but obviously, I could be wrong. If that is the case, is there a crafty way of duplicating the 12 hour call and altering the rotation accordingly?

Thank you all in advance.

Code:
<script>

const secondHand = document.querySelector('.second-hand');

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

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


/*const subHand = document.querySelector('.sub-hand');*/



function setDate() {

 const now = new Date();


 const seconds = now.getSeconds();

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

 secondHand.style.transform = `rotate(${secondsDegrees}deg)`;


 const mins = now.getMinutes();

 const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;

 minsHand.style.transform = `rotate(${minsDegrees}deg)`;


 const hour = now.getHours();

 const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;

 hourHand.style.transform = `rotate(${hourDegrees}deg)`;

 

}


setInterval(setDate, 1000);


setDate();



</script>
 
Last edited by a moderator:
Hi all, first time posting here...

I have built this analogue clock (code below) but I can't get my head around how to add an additional hand that displays a 24 hour cycle. I'm pretty sure there isn't provision for both 12 hour and 24 hour hands in javascript but obviously, I could be wrong. If that is the case, is there a crafty way of duplicating the 12 hour call and altering the rotation accordingly?

Thank you all in advance.

Code:
<script>

const secondHand = document.querySelector('.second-hand');

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

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


/*const subHand = document.querySelector('.sub-hand');*/



function setDate() {

 const now = new Date();


 const seconds = now.getSeconds();

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

 secondHand.style.transform = `rotate(${secondsDegrees}deg)`;


 const mins = now.getMinutes();

 const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;

 minsHand.style.transform = `rotate(${minsDegrees}deg)`;


 const hour = now.getHours();

 const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;

 hourHand.style.transform = `rotate(${hourDegrees}deg)`;

 

}


setInterval(setDate, 1000);


setDate();



</script>
Hi there,
So I have question: I know you mentioned "hand", but don't think got whether or not you wanted to add the hand in the clock you have already established, or create a new clock?
 
Hi Antero360,
I'm trying to create an analogue style clock that has the standard hours, mins, secs, centered in the middle, with an additional 24 hour sub-dial hand at the 6 o'clock position. CSS is taking care of the hands but I'm stuck trying to add the 24 hour javascript.

Hope that helps!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom