-1

How can I find a random location based on the moment-timezone.js? I want to show a new location every time you refresh the page.

Current code:

<hero>
    <div id="hover">
    <h2>Present time</h2>
    <div id="time"></div>
    </div>
</hero>

<script>

(function clock() {
  var now = moment().format('lll'); //local time

  /*
  I want many of these..
  moment.tz("Europe/Berlin").format('lll');
  */

  var time = document.getElementById('time');
  time.innerHTML = now;

  setInterval(clock, 1000);
})();

</script>
Scriptim
  • 1,743
  • 2
  • 20
  • 37
Martin
  • 23
  • 4

2 Answers2

0

You can get a random timezone by picking off a random element from all timezones.

var all = moment.tz.names();
var randomTimeZone = all[Math.floor(Math.random()*all.length)];
// Use it in the code as
moment.tz(randomTimeZone).format('lll');
Aakash More
  • 168
  • 1
  • 6
0

You can choose a random timezone from all available timezones:

const timezones = moment.tz.names();
const randTimezone = timezones[Math.floor(Math.random() * timezones.length)];
const now = moment.tz(randTimezone).format('lll');

document.write(randTimezone, '<br>', now);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>
Scriptim
  • 1,743
  • 2
  • 20
  • 37
  • Thank you! I am sorry, but would you mind writing it in a hastebin/pastebin please? I'm so bad in js ... :/ – Martin Mar 17 '19 at 15:02