Javascript 如何在Javascript中比较日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26477875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:56:36  来源:igfitidea点击:

How to compare datetime in Javascript?

javascriptdatetimecomparison

提问by Sata

Given
var fromDatetime = "10/21/2014 08:00:00";
var toDatetime = "10/21/2014 07:59:59";

鉴于
var fromDatetime = "10/21/2014 08:00:00";
var toDatetime = "10/21/2014 07:59:59";

Target
Need to compare this two given datetime to check if this is a valid datetime inputs.

目标
需要比较这两个给定的日期时间以检查这是否是有效的日期时间输入。

Im thinking that I could use this solution. .
var fromDatetime = new date("10/21/2014 08:00:00");
var toDatetime = new date("10/21/2014 07:59:59");

我想我可以使用这个解决方案。.
var fromDatetime = new date("10/21/2014 08:00:00");
var toDatetime = new date("10/21/2014 07:59:59");

then compare each segment
fromDatetime.getFullYear() to toDatetime.getFullYear(),
fromDatetime.getMonth() to toDatetime.getMonth(),
so on so forth until I get a non equal comparison, then return.

然后比较每个段
fromDatetime.getFullYear() 到 toDatetime.getFullYear(),
fromDatetime.getMonth() 到 toDatetime.getMonth(),
依此类推,直到我得到一个不相等的比较,然后返回。

My question is. . Is that the best case in comparing datetime in Javascript?

我的问题是。. 这是比较 Javascript 中的日期时间的最佳情况吗?

Any suggested solutions other than this will be much appreciated. . Thank you in advance.

除此以外的任何建议解决方案将不胜感激。. 先感谢您。

采纳答案by Semicolon

If you only need to know if one date is before or after another, you can use normal comparison operators:

如果您只需要知道一个日期是在另一个日期之前还是之后,您可以使用普通的比较运算符:

if (dateA > dateB) { ... }

(Note that to instantiate Date objects, it is new Date(myDateString), not new date()as in your example.)

(请注意,要实例化 Date 对象,它是new Date(myDateString),而不是new date()在您的示例中。)

This works because the Date objects will be coerced to the underlying epoch values, which are just numbers representing the count of milliseconds that have passed since Jan 1, 1970, GMT. (Relational comparison uses the result of the operands' @@toPrimitiveor valueOf()functions if available.) As a result, this will be sensitive down to the millisecond, which may or may not be desired. You can control the granularity of the comparison either by making sure the input only includes values up to the hour/minute/whatever OR by doing piecemeal comparisons like you described in your question.

这是有效的,因为 Date 对象将被强制转换为底层的 epoch 值,这些值只是表示自 1970 年 1 月 1 日格林威治标准时间以来经过的毫秒数的数字。(关系比较使用操作数@@toPrimitivevalueOf()函数的结果(如果可用)。)因此,这将敏感到毫秒,这可能是也可能不需要。您可以通过确保输入仅包含最高达小时/分钟/任何内容的值,或者通过像您在问题中描述的那样进行零碎比较来控制比较的粒度。

For more advanced date comparison operations, I highly recommend using a library like Moment.js, because natively there is not much to work with. For example Moment provides methods like

对于更高级的日期比较操作,我强烈建议使用像 Moment.js 这样的库,因为本机没有太多可使用的。例如,Moment 提供了类似的方法

moment(myDateString).startOf('hour')

which can be very helpful if you need to make comparisons only at a particular level of specificity. But none of the datetime libraries are terribly lightweight, so only use them if you are either doing serverside code or you expect to rely on them extensively. If you only need to do this one comparison, it will be wiser to roll your own solution.

如果您只需要在特定的特定级别进行比较,这将非常有用。但是没有一个 datetime 库是非常轻量级的,因此只有在您执行服务器端代码或希望广泛依赖它们时才使用它们。如果您只需要进行这一比较,那么推出自己的解决方案会更明智。