使用 angular 和 jQuery datepicker 计算天数差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26649194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating days difference with using angular and jQuery datepicker
提问by Mar
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 中,我试图使用 jQuery datepicker 计算两个选定日期之间的天数。它只返回我正在使用的函数的 null ..
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..
但是我不太确定它是函数还是在 angular 中使用 datepicker ..看起来问题是函数但是当我将它与 jQuery 一起使用时它可以工作..
my function:
我的功能:
$scope.dayDiff = function(firstDate,secondDate){
$scope.dayDifference = parseInt(Math.round((secondDate - firstDate) / (1000 * 60 * 60 * 24)));;
}
the rest: http://jsfiddle.net/1mzgLywe/5/
其余的:http: //jsfiddle.net/1mzgLywe/5/
Thanks in advance!
提前致谢!
回答by Kalhan.Toress
here is the working fiddle please check , link to fiddle
这是工作小提琴,请检查,链接到小提琴
create a function for get the dateString to pass into new Date()
创建一个函数来获取传入的 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;
}
modify the dayDiff
function as below,
修改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);
}
回答by s1moner3d
angular-momentdoes the trick! ...and (very) else more.
角力矩可以解决问题!...和(非常)其他更多。
Using the amDifferencefilter:
使用amDifference过滤器:
Get the difference between two dates in milliseconds. Parameters are date, units and usePrecision. Date defaults to current date. Example:
以毫秒为单位获取两个日期之间的差异。参数是日期、单位和 usePrecision。日期默认为当前日期。例子:
<span>Difference: {{ dateFrom | amDifference : dateTo : 'days' }} days</span>