I’ve added some custom code and its working well but I’d like to add the date and style the text. Im sure its quite simple but I can’t code and Im surprised I’ve got this far.
Here’s some custom code you can embed where you would like to show it.
<div id="clock" class="design-clock"></div>
<script>
function currentTime() {
var date = new Date(); /* creating object of Date class */
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
var day = date.toISOString().split('T')[0];
hour = updateTime(hour);
min = updateTime(min);
sec = updateTime(sec);
document.getElementById("clock").innerHTML = day + '<br>' + hour + " : " + min + " : " + sec; /* adding time to the div */
var t = setTimeout(function(){ currentTime() }, 1000); /* setting timer */
}
function updateTime(k) {
if (k < 10) {
return "0" + k;
}
else {
return k;
}
}
currentTime(); /* calling currentTime() function to initiate the process */
</script>
To add it would be better to style the parent div (the #clock div) as this would allow to paste a text inside it and actually see how it looks. Also there is no need to mention .design-clock at all in the code and even use this class anywhere, the id is enough
Aaand there is no need to even mention div in that script at all
So just create regular div inside designer, give it an ID of #clock and inside of that insert the embed with the script (for ease of reusing this particular clock snippet)
Could you tell me what the source is for the time. I’m assuming it’s the time set on the users computer? (since JS is client-side)
Basically what I would like to have is a clock above each of our locations showing the local time there. But that would be accurate regardless of where in the world it is accessed from.
If you want to display time based on timezone you just use UTC time with an offset. If you need to deal with other offsets used in different areas of the world it is best to use a library and database. Something like day.js.org.
Thank you for the response! This is purely decorative so it’s not absolutely critical if there are some edge cases where it’s wrong.
So I guess I’m wondering, is it possible to get UTC from a users computer? i.e. assuming the user has the time set correctly on their machine, and has set the correct timezone too, is there a way for the site to then work out UTC? So I could pull both british summer time and pacific time out once it’s established UTC.
So I imagine something like this: User in new york, her time is 3:00pm. The site knows that she is in new york which is utc -5 so it’s able to add 5 to get UTC which is 8pm. The site then can subtract 8 hours, to get pacific time 12noon, and display UTC, 8pm, for UK.
Although this seems like it’s going to get an order of magnitude more complicated when adding the different daylight savings times in? Not worth it for a decorative footer addition I assume?
I’m super very new to JavaScript and all of these beautiful languages…
I’ve input var hour = date.getUTCHours(); and have gotten the UTC time… however I can’t seem to nail down this offset… I’m in Lisbon PT so I believe there is a specific timezone for us to write in.