blue-knife-25988
05/27/2024, 8:56 AMbig-market-58184
05/27/2024, 2:50 PMfresh-fireman-491
05/27/2024, 6:50 PMjavascript
async function getTimeInRome() {
    const timezone = 'Europe/Rome';
    const url = `http://worldtimeapi.org/api/timezone/${timezone}`;
    try {
        const response = await axios.get(url);
        const currentTime = response.data.datetime;
        console.log(`Current time in Rome: ${currentTime}`);
    } catch (error) {
        console.error('Error fetching time:', error);
    }
}
await getTimeInRome();
Error fetching time: {"message":"Network Error","name":"AxiosError","stack":"AxiosError: Network Error\n    at l.onerror (https://studio.botpress.cloud/assets/vendor-76b6899.js:521:4465)\n    at XMLHttpRequest.r (https://studio.botpress.cloud/assets/vendor-76b6899.js:503:1525)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"adapter":["xhr","http"],"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"env":{},"headers":{"Accept":"application/json, text/plain, */*"},"withCredentials":false,"baseURL":"","method":"get","url":"https://botpress.studio/api/proxy/http%3A%2F%2Fworldtimeapi.org%2Fapi%2Ftimezone%2FEurope%2FRome"},"code":"ERR_NETWORK","status":null}
I fixed it by not using their API.
javascript
function getCurrentTime() {
  const now = new Date();
  
  now.setHours(now.getHours() + 2);
  
  const hours = now.getHours().toString().padStart(2, '0');
  const minutes = now.getMinutes().toString().padStart(2, '0');
  const seconds = now.getSeconds().toString().padStart(2, '0');
  
  const currentTime = `${hours}:${minutes}:${seconds}`;
  return currentTime;
}
console.log(getCurrentTime());
That runs without any issues and it outputs the current time for Rome.proud-eye-26408
05/27/2024, 7:05 PMDate.UTC() to make sure that the current time won't be dependent on the timezone where the code is ranfresh-fireman-491
05/28/2024, 4:33 AMblue-knife-25988
05/28/2024, 8:47 AM