使用 momentjs 检查两天内以及同一天内的时间

发布于 2025-01-14 15:37:36 字数 440 浏览 2 评论 0原文

给定以下两个没有日期且使用时刻的时间,我如何才能根据 24 小时时间最好地检查以下两个场景:

1)

let startTime = "22:00:00";
let endTime = "02:00:00";

使用 startTime 我需要设置一些标志(布尔值)来指示endTime 已跨越到第二天,并且不在 startTime 的同一 24 小时内?

2)

let startTime = "22:00:00";
let endTime = "23:53:00";

与第 (1) 项相同,但这次设置一个标志,指示 startTimeendTime 在同一 24 小时内

Given the following two times without dates and using moment, how can I best check the following two scenarios based on 24 hours times:

1)

let startTime = "22:00:00";
let endTime = "02:00:00";

Using the startTime I need to set some flag (boolean) indicating that the endTime has spanned over to the next day and not within the same 24 hour period of the startTime?

2)

let startTime = "22:00:00";
let endTime = "23:53:00";

the same as item (1) but this time setting a flag indicating that the startTime and endTime are within the same 24 hour period

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-01-21 15:37:36

这是一个片段。冒昧地将 let 替换为 const,因为变量没有预期的修改。

const startTime_1 = "22:00:00";
const endTime_1 = "02:00:00";

const momentStart_1 = moment(startTime_1, 'hh:mm:ss');
const momentEnd_1 = moment(endTime_1, 'hh:mm:ss');

const hasSpanned_1 = momentStart_1.isAfter(momentEnd_1);
console.log(`${startTime_1} -> ${endTime_1} ==> Spanned: ${hasSpanned_1}`); // true.

const startTime_2 = "22:00:00";
const endTime_2 = "23:53:00";

const momentStart_2 = moment(startTime_2, 'hh:mm:ss');
const momentEnd_2 = moment(endTime_2, 'hh:mm:ss');

const hasSpanned_2 = momentStart_2.isAfter(momentEnd_2);
console.log(`${startTime_2} -> ${endTime_2} ==> Spanned: ${hasSpanned_2}`); // true.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Here's a snippet. Took the liberty to replace let by const, as there are no expected modifications to the variables.

const startTime_1 = "22:00:00";
const endTime_1 = "02:00:00";

const momentStart_1 = moment(startTime_1, 'hh:mm:ss');
const momentEnd_1 = moment(endTime_1, 'hh:mm:ss');

const hasSpanned_1 = momentStart_1.isAfter(momentEnd_1);
console.log(`${startTime_1} -> ${endTime_1} ==> Spanned: ${hasSpanned_1}`); // true.

const startTime_2 = "22:00:00";
const endTime_2 = "23:53:00";

const momentStart_2 = moment(startTime_2, 'hh:mm:ss');
const momentEnd_2 = moment(endTime_2, 'hh:mm:ss');

const hasSpanned_2 = momentStart_2.isAfter(momentEnd_2);
console.log(`${startTime_2} -> ${endTime_2} ==> Spanned: ${hasSpanned_2}`); // true.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文