-2

How can I get the local time in my country Indonesia?

var date = new Date();
var local = date.getLocal();

I know the above code doesn't work, how do I retrieve it? I want to take (WIB) western Indonesian time / Waktu Indonesia Barat.

Please help me, all answers are like precious gold.

  • 1
    Did you try that `date.toLocaleTimeString()` – Emre Nov 27 '22 at 11:57
  • Down vote because the question does not show any research effort. The information is easily available in the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) – Yogi Nov 27 '22 at 12:30
  • Does this answer your question? [How to get the exact local time of client?](https://stackoverflow.com/questions/10659523/how-to-get-the-exact-local-time-of-client) – Donald Duck Nov 27 '22 at 13:45

2 Answers2

1

You can specify a Timezone using the toLocaleString or toLocaleTimeString

const time = new Date().toLocaleTimeString('en-US', { timeZone: 'Asia/Jakarta' });
console.log(time);
RenaudC5
  • 3,553
  • 1
  • 11
  • 29
0

Use a third party API to show time from a specific country. You can use API from worldtimeapi.org/. Make an ajax call, get the time of your desired location. You can use plain javascript or use any ajax library to do that. Here I'm doing it in plain javascript

function getTime(url) {
    return new Promise((resolve, reject) => {
        const req = new XMLHttpRequest();
        req.open("GET", url);
        req.onload = () =>
            req.status === 200
                ? resolve(req.response)
                : reject(Error(req.statusText));
        req.onerror = (e) => reject(Error(`Network Error: ${e}`));
        req.send();
    });
}

Now Use this function to make the ajax call

let url = "http://worldtimeapi.org/api/timezone/Pacific/Auckland";

getTime(url)
    .then((response) => {
        let dateObj = JSON.parse(response);
        let dateTime = dateObj.datetime;
        console.log(dateObj);
        console.log(dateTime);
    })
    .catch((err) => {
        console.log(err);
    });
raiyan22
  • 1,043
  • 10
  • 20