Moment js 没有正确给出天数
Moment js is not giving days properly
我正在使用 angular 和 moment js 来获取日期差异,但它没有给出正确的天数,
module.controller("MainCtrl", ['$scope', '$mdDatePicker', function($scope, $mdDatePicker) {
this.startDate = function(ev) {
$mdDatePicker(ev, $scope.startDate).then(function(selectedDate) {
$scope.startDate = selectedDate;
var now = selectedDate;
var then = $scope.endDate;
var ms = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
$scope.dateDifferenceDays = d.asDays();
});
}
this.endDate = function(ev) {
$mdDatePicker(ev, $scope.endDate).then(function(selectedDate) {
$scope.endDate = selectedDate;
var now = $scope.startDate;
var then = selectedDate;
var ms = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
$scope.dateDifferenceDays = d.asDays();
});
}
}]);
这是我正在开发的完整代码笔 - Codepen
您不需要为此使用持续时间。直接使用 diff 的第二个参数来获取天数的差异:
$scope.dateDifferenceDays = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"), 'days');
我正在使用 angular 和 moment js 来获取日期差异,但它没有给出正确的天数,
module.controller("MainCtrl", ['$scope', '$mdDatePicker', function($scope, $mdDatePicker) {
this.startDate = function(ev) {
$mdDatePicker(ev, $scope.startDate).then(function(selectedDate) {
$scope.startDate = selectedDate;
var now = selectedDate;
var then = $scope.endDate;
var ms = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
$scope.dateDifferenceDays = d.asDays();
});
}
this.endDate = function(ev) {
$mdDatePicker(ev, $scope.endDate).then(function(selectedDate) {
$scope.endDate = selectedDate;
var now = $scope.startDate;
var then = selectedDate;
var ms = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
$scope.dateDifferenceDays = d.asDays();
});
}
}]);
这是我正在开发的完整代码笔 - Codepen
您不需要为此使用持续时间。直接使用 diff 的第二个参数来获取天数的差异:
$scope.dateDifferenceDays = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"), 'days');