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?
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}`);
});