javascript get current timezone code example

Example 1: js get iana timezone

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

Example 2: javascript set date to timezone

var d = new Date("2020-04-13T00:00:00.000+08:00"); /* midnight in China on April 13th */
d.toLocaleString('en-US', { timeZone: 'America/New_York' });
//=> "4/12/2020, 12:00:00 PM"
// (midnight in China on April 13th is noon in New York on April 12th)

Example 3: client timezone offset in JavaScript

var clientTimezoneOffset = new Date().getTimezoneOffset()/60;//offset in hours

Example 4: get browser timezone javascript

Intl.DateTimeFormat().resolvedOptions().timeZone

Example 5: how to see my timezone using js

document.getElementById("time").innerHTML = Date().slice(Date().indexOf("(")+1,Date().lastIndexOf(")"));

Example 6: javascript date timezone

// this example takes 2 seconds to run
const start = Date.now();

console.log('starting timer...');
// expected output: starting timer...

setTimeout(() => {
  const millis = Date.now() - start;

  console.log(`seconds elapsed = ${Math.floor(millis/1000)}`);
  // expected output : seconds elapsed = 2
}, 2000);

Tags:

Php Example