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

202
Vistas
How do you tell if a datetime is am or pm using luxon / moment?

This:

import {DateTime} from "luxon";

[
   "2022-05-30T11:00:00Z",
   "2022-05-30T11:00:00+01:00",
   "2022-05-30T11:00:00-01:00",
].map(raw => {
   var sysTime = DateTime.fromISO(raw);
   var amPm = sysTime.hour < 12 ? "am" : "pm";
   console.log(`${raw}: ${amPm}`);
});

Emits this:

2022-05-30T11:00:00Z: pm
2022-05-30T11:00:00+01:00: pm
2022-05-30T11:00:00-01:00: pm

Which is clearly wrong.

Yes, it is wrong, because 2022-05-30T11:00:00+01:00 is not pm in that timezone.

Since luxon automatically stores SystemZone, you have to explicitly convert it to correct timezone, if you happen to know what it is, for example:

[
   "2022-05-30T11:00:00Z",
   "2022-05-30T11:00:00+01:00",
   "2022-05-30T11:00:00-01:00",
].map(raw => {
   var sysTime = DateTime.fromISO(raw);
   var realTime = sysTime.setZone('UTC+1'); // <---- explicit
   var amPm = realTime.hour < 12 ? "am" : "pm";
   console.log(`${raw}: ${amPm}`);
});

Emits this:

2022-05-30T11:00:00Z: pm <---- still wrong
2022-05-30T11:00:00+01:00: am <--- correct
2022-05-30T11:00:00-01:00: pm

So, what's the right way to do this?

...and to be clear, I'm not trying to parse these date times without a timezone, I'm trying to parse them in a way that preserves the "UTC+X" timezone that is in the raw date string.

Is that possible?

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

0

use: setZone: true in fromISO.

import {DateTime} from "luxon";

[
   "2022-05-30T11:00:00Z",
   "2022-05-30T11:00:00+01:00",
   "2022-05-30T11:00:00-01:00",
].map(raw => {
   var sysTime = DateTime.fromISO(raw, {setZone: true});
   var amPm = sysTime.hour < 12 ? "am" : "pm";
   console.log(`${raw}: ${amPm}`);
});
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