【问题标题】:MomentJs Invalid Date after add some days添加几天后MomentJs无效日期
【发布时间】:2019-03-01 15:01:23
【问题描述】:

我正在使用 angularJS 和 momentJs 实现日期组件,我想在计数器之后增加日期的日期。当我想添加 40 天时,它正在工作,但在 29/04/2019 之后,日期已转换为“无效日期”。

看起来配置会出现此错误。如果您删除它,它会起作用。但是我需要这个配置,我不能删除它。

在这里你可以找到sn-p代码:

// Code goes here

var app = angular.module('App', ['ngMaterial', 'ngMessages', 'ngAnimate']);
app.controller('MainCtrl', ['$rootScope', '$scope',
  function($rootScope, $scope) {

    $scope.DateValide = new Date();
    $scope.Validite = 10;
    $scope.minDate = new Date();


    $scope.DateValideChange = function() {
      $scope.Validite = dateDiff(new Date(), $scope.DateValide);
    };

    $scope.ValiditeChange = function() {
      if ($scope.Validite) {
        console.log("Old : " + $scope.DateValide);
        $scope.DateValide = new Date(moment($scope.Validite, "dd-mm-yyyy").add($scope.Validite, "days"));
        console.log("New : " + $scope.DateValide);
      }
    };

    function dateDiff(dateold, datenew) {
      var oneDay = 24 * 60 * 60 * 1000;
      return Math.round(Math.abs((dateold.getTime() - datenew.getTime()) / (oneDay)));
    }


  }
]);

app.config(function($mdDateLocaleProvider) {
  $mdDateLocaleProvider.formatDate = function(date) {
    return date ? moment(date).format("DD-MM-YYYY") : "";
  };

  $mdDateLocaleProvider.parseDate = function(dateString) {
    var m = moment(dateString, 'DD-MM-YYYY', true);
    return m.isValid() ? m.toDate() : new Date(NaN);
  };
});
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.css" />
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.min.js"></script>
  
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="App">
  <div ng-controller="MainCtrl" class="col-sm">
    <md-input-container>
      <label>Date de validite</label>
      <md-datepicker ng-model="DateValide" ng-change="DateValideChange()"></md-datepicker>
    </md-input-container>
    <md-input-container class="col-2">
      <label>Validite</label>
      <input type="number" ng-model="Validite" ng-change="ValiditeChange()" />
    </md-input-container>
  </div>
</body>

</html>

【问题讨论】:

  • 请注意,时刻 parsing 令牌区分大小写(mm 表示分钟,MM 表示月份),您可以使用 toDate() 获取Moment.js 包装的原生 Date 对象

标签: javascript angularjs date momentjs


【解决方案1】:

// Code goes here

var app = angular.module('App', ['ngMaterial', 'ngMessages', 'ngAnimate']);
app.controller('MainCtrl', ['$rootScope', '$scope',
  function($rootScope, $scope) {

    $scope.DateValide = new Date();
    $scope.Validite = 10;
    $scope.minDate = new Date();


    $scope.DateValideChange = function() {
      $scope.Validite = dateDiff(new Date(), $scope.DateValide);
    };

    $scope.ValiditeChange = function() {
      if ($scope.Validite) {
        console.log("Old : " + $scope.DateValide);
        $scope.DateValide = new Date(moment($scope.minDate, "dd-mm-yyyy").add($scope.Validite, "days"));
        console.log("New : " + $scope.DateValide);
      }
    };

    function dateDiff(dateold, datenew) {
      var oneDay = 24 * 60 * 60 * 1000;
      return Math.round(Math.abs((dateold.getTime() - datenew.getTime()) / (oneDay)));
    }


  }
]);

app.config(function($mdDateLocaleProvider) {
  $mdDateLocaleProvider.formatDate = function(date) {
    return date ? moment(date).format("DD-MM-YYYY") : "";
  };

  $mdDateLocaleProvider.parseDate = function(dateString) {
    var m = moment(dateString, 'DD-MM-YYYY', true);
    return m.isValid() ? m.toDate() : new Date(NaN);
  };
});
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.css" />
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.min.js"></script>
  
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="App">
  <div ng-controller="MainCtrl" class="col-sm">
    <md-input-container>
      <label>Date de validite</label>
      <md-datepicker ng-model="DateValide" ng-change="DateValideChange()"></md-datepicker>
    </md-input-container>
    <md-input-container class="col-2">
      <label>Validite</label>
      <input type="number" ng-model="Validite" ng-change="ValiditeChange()" />
    </md-input-container>
  </div>
</body>

</html>

我认为你的行中有错字

$scope.DateValide = new Date(moment($scope.Validite, "dd-mm-yyyy").add($scope.Validite, "days"));

应该是:

$scope.DateValide = new Date(moment($scope.minDate, "dd-mm-yyyy").add($scope.Validite, "days"));

【讨论】:

  • 天啊...我觉得自己很笨。谢谢你。
猜你喜欢
  • 2014-04-28
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多