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

360
Visualizações
How to use Moment.JS to convert to 12 hour clock or 24 hour clock based on timezone

So I'm starting with a REALLY weird format, which is essentially the minutes after midnight represented as a number (in the timezone I'm trying to get to). So if I'm in New York and mean 6:30 AM, the starting value is 390.

Here's my code so far:

const open_time = (String(startingTime / 60)).split(".") // startingTime of 390 becomes an array of ['6','5']
const open_hour = open_time[0] // open hour becomes '6'
var open_minutes = 0 // if the startingTime was evenly divisible by 60, that means we're on the hour
if (open_time[1] !== undefined) { // startingTime wasn't evenly divisible by 60, need to do some calculations
    open_minutes = Number(`0.${open_time[1]}`) * 60 // get the minutes into the hour
}
const open_minutes_formatted = open_minutes.toLocaleString('en-US', {
    minimumIntegerDigits: 2,
    useGrouping: false
}) // make sure the minutes format in 00 - 09 instead of 0 - 9 for the first 10 minutes of the hour.
const hours = `${open_hour}:${open_minutes_formatted}` // final string outputted

At this point, hours is formatted to something like 6:30, or if it's in the afternoon, 15:00. My question is, how do I use moment.js to format this time based on the 12 hour clock vs 24 hour clock based on where the user is?

At this point in the code, I've used the module tzlookup to get the timezone for where the user is located so I think I can use that, but not sure how the actual conversion looks. Any help appreciated!

about 4 years ago · Juan Pablo Isaza
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