I would like to put on my website a clock with the time in real time in Barcelona. I've gotten some code inside a text box to work (without using CSS), but it only shows the hour/minutes/seconds at the moment you enter the page, and the seconds don't advance. I have seen that there is code to refresh the seconds every second, but I don't know how to integrate it in my code to make it work. Thank you very much
<p style="text-align: left;”>:BARCELONA<span id="datetime"></span><script>
var dt = new Date(); document.getElementById("datetime").innerHTML = (("0"+dt.getHours()).slice(-2)) +":"+ (("0"+dt.getMinutes()).slice(-2)) +":"+ (("0"+dt.getSeconds()).slice(-2));
</script></p>
In order to do what you're trying to do, you need to understand a bit about what UTC means and how to work with those values.
In the case of your question, you do not make it clear if you are referring to Barcelona, Spain or Barcelona, Venezuela , therefore I will create both, so that the process is well understood.
First of all we must establish the time difference between UTC and the local time of each region.
Spain works in the time zone known as GMT+01:00, and Venezuela works in the time zone known as GMT-04:00.
This means that the time in Spain is 1 unit (hour) higher than UTC (old GMT) and the time in Venezuela is 4 units lower than UTC.
Therefore if the UTC time is let's say:
12:00:00 GMT
, then:13:00:00 GMT+01:00
08:00:00 GMT-04:00
The object
Date
allows us to obtain the values of hours, minutes and seconds, both local and UTC.Let's look at an example:
Now, since we already have a way to obtain the UTC time and we also know the time difference of the country with respect to UTC, we can show a clock from any time zone, it is just a matter of calculating the value that said clock would have.
Sticking to the JS standard for the object
Date
, any time zone that is east (right) of UTC will be written as negative integer values. Therefore, Spain corresponds to -1.Conversely, any time zone that is west (left) of UTC will be written with positive values. Therefore, Venezuela corresponds to 4.
Now, I am going to write a reusable method, which will receive as parameters an object from the DOM that will represent the clock (a particular object whose example is below), and also the value (integer type) of the time difference of the country that we want to use. for our watch.
With this we have, however, a small problem in our method.
If we are at 23 hours UTC, the time in Spain will be 24, but a clock only goes from 0 to 23 hours, therefore 24 will not be a valid time.
If we are at 0 hours UTC, the time in Venezuela will be -4, which is also invalid.
To fix this we need to make a small correction to our method.
As they would say in Spain: that's cool , and as they would say in Venezuela: that's papayita .
Let's see what happens here, if the UTC time is 0, then the calculated time for Venezuela is:
0 - 4 = -4
. Therefore, when making the indicated correction we have that:Which is correct, when UTC is 0, the time in Venezuela is 20 or, as it is known there , 8 at night .
On the other hand, if UTC is 23, then the calculated time for Spain is:
23 - (-1) = 23 + 1 = 24
. When applying the indicated correction we have:Which is correct, since when in UTC it is 23 hours, in Spain it is 0 hours.
With all this we already have a way to calculate the time of any country using this peculiar method that writes the appropriate values in a DOM object.
It is then necessary to update these values every second (so that it behaves like an ordinary clock). For this we will use the method
setInterval()
. To which we will pass the function that updates the clock in the DOM, the time parameter in milliseconds (1 second = 1000 milliseconds) and the additional parameters for the execution of the function.For example:
With this our function will be executed every second, resulting in a clock that varies every second from the moment the page is loaded, showing the local time of the country that is used as the time zone.
The following implementation uses a modified version to display both clocks (Spain and Venezuela), but builds on everything above:
I hope this clears your doubt.
You can use this code, which does something similar. It works perfectly. In the body in the onload event it makes a function call. This function in turn calls itself with setInterval.
https://stackoverflow.com/a/53383833/5903042
A greeting.
I put the complete code. A greeting, to copy and paste in an html file. Greetings.