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

107
Visualizações
Printing Time without leading zero in Javascript

I'm pretty sure this is simple but I am trying to print out the current time in 12-hour format without including the leading zero in the time string. So, for example.

09:30 AM should print as 9:30 AM.

The current code I have so far (which prints the version with the leading zero) is this:

var d = new Date();
var timeString = d.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});

How can I change the above code to remove the leading zeros when they appear?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your code is specifying:

hour: '2-digit'

Just get rid of that and all is well. :-)

For the US locale, here's what I get:

const d = new Date();
console.log(
  d.toLocaleTimeString() // 8:40:28 PM
);

See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString#using_options

about 4 years ago · Juan Pablo Isaza Relatório

0

Use the numeric format for the hour instead of 2-digit

var timeString = d.toLocaleTimeString([], {hour: 'numeric', minute:'2-digit'});

const d = new Date();
d.setHours(9, 30);

console.log(d.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}));
console.log(d.toLocaleTimeString([], {hour: 'numeric', minute:'2-digit'}));

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