片刻js没有给予适当的日子

时间:2015-05-30 08:53:30

标签: javascript angularjs momentjs

我使用角度与时刻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

1 个答案:

答案 0 :(得分:0)

您不需要使用持续时间。直接使用diff的第二个参数得到天数:

$scope.dateDifferenceDays = moment(now, "DD/MM/YYYY HH:mm:ss").diff(moment(then, "DD/MM/YYYY HH:mm:ss"), 'days');