如何比较日期并检查是未来还是过去

发布于 2025-01-13 15:29:17 字数 89 浏览 0 评论 0原文

我的日期格式为 3/13/2022 05:44 PM。 如何将此日期与以这种方式格式化的其他日期进行比较,并检查它们是在相对过去还是未来?

I have a date formatted as 3/13/2022 05:44 PM.
How can I compare this date with the other dates which are formatted in this manner and check whether they are in the comparative past or future?

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

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

发布评论

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

评论(1

ˉ厌 2025-01-20 15:29:17

哦,所以你需要比较两个日期并找到最新的、过去的或未来的。

所以,你可以使用 Date.parse() 函数将日期转换为毫秒。这将解析日期字符串并返回自 1970 年 1 月 1 日午夜以来的毫秒数。

然后相应地比较这两个数字。例如:

<p id="demo"></p>

<script>
let ms = Date.parse("3/13/2022 05:47 PM");
let ns = Date.parse("3/13/2022 04:47 AM");
if(ms > ns){
document.getElementById("demo").innerHTML = "3/13/2022 05:47 PM is Future";
}else{
document.getElementById("demo").innerHTML = "3/13/2022 04:47 AM is Future";
}

Ohkay, so you need to compare the two dates and find which is latest, that is past or future..

So, you can just convert the dates to milliseconds using the Date.parse() function. This will parses a date string and returns the number of milliseconds since midnight January 1, 1970.

And then compare the two numbers accordingly.. For example:

<p id="demo"></p>

<script>
let ms = Date.parse("3/13/2022 05:47 PM");
let ns = Date.parse("3/13/2022 04:47 AM");
if(ms > ns){
document.getElementById("demo").innerHTML = "3/13/2022 05:47 PM is Future";
}else{
document.getElementById("demo").innerHTML = "3/13/2022 04:47 AM is Future";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文