Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!