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

213
Vistas
How to get hour and minute as numeric values (not string) in local timezone from Javascript Date object?

I have a Date in UTC format, and want to get the hour and minute but in the local time zone the browser is running in - and without me specifying the timezone explicitly, just using the default used by the browser. I know I can get the whole date time in the local zone as a string using Date.toLocaleTimeString() or Date.toString() but don't want to have to parse out the hour and minute.

Also I can get the numeric hour and minute from a Date using Date.getHours() and getMinutes(), but this won't be in the local time zone - or will it? (ironically my local timezone is UTC so not sure how to test)

Thanks

EDIT: Answering my own question, see below

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I would use toLocalstring and then i would convert string to number.

function getNumericHourAndMinute() {
  const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
  const d = new Date().toLocaleString('en-US', { timeZone: tz,  timeStyle: 'short', hour12: false });
  const p = d.split(':');
  
  return [Number(p[0]), Number(p[1])];
  
}
console.log(getNumericHourAndMinute());

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Date.prototype.getHours and Date.prototype.getMinutes to get the hours and minetes from a string date like: 2022-01-08 14:00:00.

Use this code:

var d = new Date(/* A example date: '2022-01-08 14:00:00'*/);

var h = d.getHours();
var m = d.getMinutes();

// Whit the example date you get in the console: 'Time: 14:00.'
console.log('Time: '+ h + ' : ' + m + '.');
about 4 years ago · Juan Pablo Isaza Denunciar

0

So a Javascript Date object is apparently not in a timezone as such, it's basically just the number of milliseconds since the Unix Epoch as per Date.now() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

So what I didn't realise until I checked the docs ;-) was that Date.getHours() returns the numeric hours value according to local time (i.e. time in the timezone of the device running the code) and not UTC time (which I was trying to avoid). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours

So Date.hours() gives me exactly what I want (if for some reason I did want UTC, I could use Date.getUTCHours() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours

Then I can just use Date.getMinutes() to get the minute value of the Date and that's not affected by timezone anyway so all good :)

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