简体   繁体   English

Moment JS没有正确地提供日子

[英]Moment js is not giving days properly

I am using angular with moment js to get date difference but it is not giving right number of days, 我正在使用带有js矩的angular来获取日期差,但它没有给出正确的天数,

  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();
      });
    }
  }]);

Here is full codepen i am developing - Codepen 这是我正在开发的完整Codepen- Codepen

You don't need to use duration for this. 您不需要为此使用持续时间。 Directly use the second parameter of diff to get the difference as days : 直接使用diff的第二个参数获取天数的差值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM