gpt4 book ai didi

javascript - 添加几天后 MomentJs 无效日期

转载 作者:行者123 更新时间:2023-12-01 01:13:42 27 4
gpt4 key购买 nike

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

看起来配置出现了这个错误。如果你删除它,它就会起作用。但我需要这个配置,但无法删除它。

在这里您可以找到片段代码:

// 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>

最佳答案

// 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"));

关于javascript - 添加几天后 MomentJs 无效日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com