简体   繁体   English

您如何使用 luxon / moment 判断日期时间是上午还是下午?

[英]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 .是的,这错误的,因为2022-05-30T11:00:00+01:00那个时区不是pm

Since luxon automatically stores SystemZone , you have to explicitly convert it to correct timezone, if you happen to know what it is, for example:由于 luxon 自动存储SystemZone ,因此如果您碰巧知道它是什么,则必须将其显式转换为正确的时区,例如:

[
   "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 . ...并且要清楚,我不是试图在没有 timezone 的情况下解析这些日期时间,而是试图以保留原始日期字符串中的“UTC+X”时区的方式解析它们。

Is that possible?那可能吗?

use: setZone: true in fromISO .使用: setZone: truefromISO中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有AM / PM的moment.js格式化没有时区(Z)的日期时间 - How to format datetime without timezone (Z) using moment.js with AM/PM 使用 date-fns 2.x 版时,如何将日期时间格式化为 am/pm,没有句点? - How do you format a datetime to am/pm, without periods, when using date-fns version 2.x? 如何使用moment.js计算从下午到下午的小时数? - How do I calculate hours from PM to AM using moment.js? 如何以 12 小时 AM/PM 格式显示 JavaScript 日期时间? - How do you display JavaScript datetime in 12 hour AM/PM format? 如何使用moment js从日期时间字符串中获取am pm - How to get am pm from the date time string using moment js 使用moment.js获取AM或PM的当前时间 - fetch current time in AM or PM using moment.js 为什么我在下面的 Luxon/Moment 代码中得到“Invalid DateTime”? - Why is that I'm getting "Invalid DateTime" in the following Luxon/Moment code? 如何在Moment JS中使用带有日历功能的AM / PM格式 - How to use AM/PM format with the calendar function in moment js 使用Moment.js,如何将时刻转换为日期或进行日历日期比较? - Using Moment.js, how do you convert a moment to a date or do calendar date comparisons? 在 Luxon 中使用特定格式将字符串格式化为日期时间 - Formatting Strings to Datetime using specific formats in Luxon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM