I have a clock like so:
const timeContainer = document.querySelector('.timeContainer');
var showTime = (timeZone) => {
let utcTime = new Date().toUTCString();
//need to subtract/add timezone from utcTime
//need to remove everything except for time e.g. 00:22:22
return utcTime;
};
setInterval( () => {
timeContainer.innerHTML = showTime('-8');
}, 1000);
Which gives me GMT time like this:
Tue, 29 Nov 2016 00:35:54 GMT
.. which is what I want! But say I want to get the time in Hong Kong, and also only the numerical time, none of the date and timezone information.
I basically need to:
a) subtract/add a variable timeZone integer from utcTime
b) remove all text in output except for the time, e.g. utcTime only outputs as 00:22:22
I basically need to generate a clock based on what timeZone integer I run showTime with, for example:
showTime(-5);
//returns time in EST timezone
showTime(-6);
//returns time in CST timezone
etc. Is there a way to do this using .toUTCString()? Or is there a better method?
Here's a jsFiddle with my code.