【问题标题】:How to check in AngularJs if some dates are greater then others如果某些日期大于其他日期,如何检查 AngularJs
【发布时间】:2020-05-01 13:00:51
【问题描述】:

我需要检查某些日期字段是否大于其他日期字段。 我以这种方式解决了只有几个日期。我找不到像这样的多个变量的解决方案:

$scope.date1startFrom;
$scope.date1startTo;
$scope.date2startFrom;
$scope.date2startTo;
$scope.date3startFrom;
$scope.date3startTo;

只有当这对夫妇的元素具有价值时,我才必须看到警报。我找到了这个解决方案,但我想将它扩展到多个值,但我不能。

var datesAreDefined =  $scope.date1startFrom && $scope.date1startTo;
if (datesAreDefined && !periodFromAndToIsValid($scope.date1startFrom,  $scope.date1startTo)) {
    alert("error");
}

function periodFromAndToIsValid (from, to){
    var fromDate = new Date(from).getTime();
    var toDate = new Date(to).getTime();
    return toDate >= fromDate;
}

【问题讨论】:

    标签: javascript jquery angularjs


    【解决方案1】:

    我建议使用 MomentJS 之类的东西,它对这种情况非常有效:

    https://momentjs.com/

    在此链接中,您可以找到轻松比较日期的方法。 https://momentjs.com/docs/#/query/

    【讨论】:

      猜你喜欢
      • 2015-12-19
      • 2016-03-06
      • 1970-01-01
      • 2021-12-28
      • 2015-01-08
      • 2016-09-25
      • 2021-08-23
      • 2012-05-25
      • 1970-01-01
      相关资源
      最近更新 更多