I am working on my new website for my freelancing business, and I thought showing my local time would be a neat feature for those interested in reaching out to me.
This is the current code I have so far, but would like to expand/modify it a bit:
<script>
function currentTime() {
var date = new Date();
var hour = date.getUTCHours() + 2;
var min = date.getMinutes();
if (min < 10)
min = "0" + min;
var ampm = hour >= 12 ? 'pm' : 'am';
document.getElementById("clock").innerText = hour + ':' + min + ampm;
var t = setTimeout(function () { currentTime() }, 1000);
}
currentTime();
</script>
There are two questions on my mind with this feature, though, so I decided to drop by to see if anyone was willing to help me out!
- Is there any alternative to
date.getUTCHours() + 2
which would eliminate the need from manually preparing it for summertime and vice-versa? - How would I display a specific sentence if the time is X? For example, if it’s late and I’m likely to be asleep and the other way around.