Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

361
Visualizações
How to convert UTC hours to local using Javascript

I want to convert UTC hours to local time using JavaScript it is possible ?

I have last 24 hours in array

let _arrHours = ["4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","1","2","3"]

I read this question Convert Hour UTC to Local using moment.js

It's possible using simple JavaScript ?

I have try to fix it but it's works only India time ? what do for other countries ?

  let _arrHours = ["4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "1", "2", "3"];
    let _arrDt = [];
    for (let t = 0; t < _arrHours.length; t++) {
      let tm = _arrHours[t];
      tm = parseInt(tm) + 5.5; //indian timezone, I want to do it dynamic country wise !
      if (tm > 24) {
        tm = tm - 24;
      }
      else if (tm < 0) {
        tm = tm + 24;
      }
      _arrDt.push(tm);
    }

    console.log(_arrDt, '_arrDt'); 
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using new Date().getTimezoneOffset() you can get offset then convert into hour !

Try this code it's help you !

 let offset = new Date().getTimezoneOffset(); //get the country wise offset !
    if (offset > 0) { //let's convert in to hour
      offset = (-Math.abs(offset / 60));
    } else {
      offset = (Math.abs(offset / 60));
    }
    let _arrHours = ["4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "1", "2", "3"];
    let _arrDt = [];
    for (let t = 0; t < _arrHours.length; t++) {
      let tm = _arrHours[t];
      tm = parseInt(tm) + offset;
      if (tm > 24) {
        tm = tm - 24;
      }
      else if (tm < 0) {
        tm = tm + 24;
      }
      _arrDt.push(tm);
    }

    console.log(_arrDt, '_arrDt');
about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply use toLocaleTimeString to get local time

let assume you UTC hours is 17

let UTCTime=17:00;

var LocalTime=new Date('2021-09-23T'+ UTCTime+ "z").toLocaleTimeString()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda