【问题标题】:Calculating days difference with using angular and jQuery datepicker使用 Angular 和 jQuery datepicker 计算天数差异
【发布时间】:2014-10-30 09:24:28
【问题描述】:

在角度方面,我正在尝试使用 jQuery datepicker 计算两个选定日期之间的天数。它只返回 null 与我正在使用的函数..

但是我不太确定它是函数还是在角度中使用 datepicker..看起来问题出在函数上,但是当我将它与 jQuery 一起使用时它可以工作..

我的功能:

$scope.dayDiff = function(firstDate,secondDate){

    $scope.dayDifference = parseInt(Math.round((secondDate - firstDate) / (1000 * 60 * 60 * 24)));;
}

其余的:http://jsfiddle.net/1mzgLywe/5/

提前致谢!

【问题讨论】:

  • 您正在尝试减去字符串,这将不起作用。 secondDatefirstDate 是字符串类型而不是日期。

标签: jquery angularjs datepicker


【解决方案1】:

这是工作小提琴,请检查,link to fiddle

创建一个函数来获取 dateString 以传递给new Date()

$scope.formatString = function(format) {
    var day   = parseInt(format.substring(0,2));
    var month  = parseInt(format.substring(3,5));
    var year   = parseInt(format.substring(6,10));
    var date = new Date(year, month-1, day);
    return date;
}

修改dayDiff函数如下,

$scope.dayDiff = function(firstDate,secondDate){
    var date2 = new Date($scope.formatString(secondDate));
    var date1 = new Date($scope.formatString(firstDate));
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());   
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    alert(diffDays);
}

【讨论】:

    【解决方案2】:

    angular-moment 成功了! ...和(非常)其他更多。

    使用 amDifference 过滤器:

    以毫秒为单位获取两个日期之间的差异。参数是 日期、单位和使用精度。日期默认为当前日期。示例:

    <span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span>
    

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多