Lucas03
Lucas03

Reputation: 2347

javascript clock with timezone

would like to show time on my site with seconds counting, like this one. I know there is a lot of similar questions, but I was not able to find out a solution that would work for me. Problem is, all those javascript clock that handle timezone are done by offset.

I am however using django timezones: "UTC", "Asia/Harbin", ... Javascript on default creates new date from local time, but I allow user to change his/hers timezone. How do I pass this changed timezone to javascript? I tried moment-timezone, but it somehow ignores .tz().

<script>
function startTime() {
    document.getElementById('time').innerHTML = moment().tz("{{ TIME_ZONE }}").format('HH:mm:ss');
    var t = setTimeout(function(){startTime()},500);
}
</script>

This way time is running, but changing timezone has no impact on time. How do I do this properly?

Upvotes: 2

Views: 735

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241430

Likely the problem is that you are loading moment-timezone without any data. Try using one of the prepackaged bundles on the moment-timezone site, such as moment-timezone-2010-2020.js, or moment-timezone-all-years.js.

See the moment-timezone docs.

Upvotes: 1

Related Questions