0

I want to create output that matche my computer ( macOS ) as close as possible.

My computer's time looks like this:

enter image description here

I don't need the AM or PM.

I just need hour:minute:second

I looked at getTime() and now() but did not see an obvious solution.

What is the most straightforward and easy way to do this?

2 Answers2

1

You can try :

const clock = document.querySelector('where you want to output the time');

function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const minutes = now.getMinutes();
const hours = now.getHours();
clock.innerHTML = hours + ":" + minutes + ":" + seconds;
}

setInterval(setDate, 1000);

new Date() Allows you to get the current time

getSeconds(), getMinutes(), getHours() Allows you to get the seconds, minutes and hours out of the date you put before. Here the date is now and you refresh the clock every second via setInterval(setDate, 1000) so you basically refresh & display the current time every second with that function.

ka_raph
  • 36
  • 4
0

You can get the current time with new Date() and then you can call .getHours(), .getMinutes(), and .getSeconds() on that object to get the hour, minute, and second components.

duskwuff -inactive-
  • 171,163
  • 27
  • 219
  • 269