Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

355
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda