javascript Angular JS 比较两个日期是否相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27547644/
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-10-28 07:36:56  来源:igfitidea点击:

Angular JS compare two Dates are equal or not

javascriptangularjsdateangular-ng-if

提问by Shylendra Madda

I have used this angular code to compare two dates are equal or not.

我使用这个角度代码来比较两个日期是否相等。

Code:

代码:

<div class="batchPlannings">
  <div ng-repeat="batchPlanning in batchPlannings"
    ng-animate="'animate'">
    <div ng-switch="batchPlanning.status">
      <div ng-switch-when="MAPPED" class="my-switch-animation">
        <p>start date: {{batchPlanning.startDate}}</p>
        <p>placement date: {{placementDate}}</p>
        <div ng-if="batchPlanning.startDate === {{placementDate}}"
          class="my-switch-animation">
          <a th:href="@{/placement}" class="list-group-item"
            sec:authorize="hasAnyRole('ROLE_SYSTEM_ADMIN', 'ROLE_CENTER_ADMIN')">Batch
            with batch code '{{batchPlanning.batchCode}}' is ready for
            placements</a>

        </div>
      </div>
    </div>
  </div>
</div>

Here I wanna check test those two dates or equal or not I am getting those dates perfect and fine and those are also equal but unable to make that condition as true. I checked it with == as well but no result.

在这里,我想检查测试这两个日期或相等与否我得到的这些日期完美无缺,这些日期也相等但无法使该条件成立。我也用 == 进行了检查,但没有结果。

Any help or suggestion will be appreciated.

任何帮助或建议将不胜感激。

回答by Reactgular

JavaScript uses objects to store date values. When you want to compare if two dates are equal don't do this date1 === date2. That will just tell you if the two variables reference the same object.

JavaScript 使用对象来存储日期值。当你想比较两个日期是否相等时,不要这样做date1 === date2。这只会告诉你两个变量是否引用了同一个对象。

To check if two dates are equal use the getTime()method of the date object. Like this:

要检查两个日期是否相等,请使用getTime()日期对象的方法。像这样:

date1.getTime() == date2.getTime()

date1.getTime() == date2.getTime()

Be careful because any difference in seconds will fail to match the dates.

请小心,因为任何秒数差异都将无法与日期匹配。

Now ng-ifwill accept AngularJS expressions. You have this in your example batchPlanning.startDate === {{placementDate}}. Which is an invalid expression. {{placementDate}}is syntax used to create a binding.

现在ng-if将接受 AngularJS 表达式。你的例子中有这个batchPlanning.startDate === {{placementDate}}。这是一个无效的表达式。{{placementDate}}是用于创建绑定的语法。

What you want to do is batchPlanning.startDate.getTime() == placementDate.getTime().

你想要做的是batchPlanning.startDate.getTime() == placementDate.getTime()

Ensure that both variables are valid JavaScript date objects.

确保两个变量都是有效的 JavaScript 日期对象。

I recommend that you use the momentjavascript library for date processing. It makes working with dates a lot easier.

我建议您使用momentjavascript 库进行日期处理。它使处理日期变得更加容易。

http://momentjs.com/

http://momentjs.com/