计算天差用角和jQuery日期选择器日期、选择器、天差用角、jQuery

2023-09-13 02:40:40 作者:所谓坚强╮都是伪装

在转角,我试图用使用jQuery日期选择器来计算两个所选日期之间的天数。它仅返回空与我使用的功能。

In angular, I am trying to calculate the number of days between two selected date with using jQuery datepicker. It returns only null with the function that I am using..

不过,我不太清楚,如果它是在angular..It功能,或者使用日期选择器看起来像问题是功能,但它的工作原理,当我与jQuery使用它。

However I am not quite sure if it is the function or using datepicker in angular..It looks like the problem is function but it works when I use it with jQuery..

我的功能:

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

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

休息: http://jsfiddle.net/1mzgLywe/5/

在此先感谢!

推荐答案

这里是工作提琴请检查一下,链接拨弄

here is the working fiddle please check , link to fiddle

创建得到dateString一个函数传递到新的Date()

create a function for get the dateString to pass into 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);
}