使用 angular 和 jQuery datepicker 计算天数差异 [英] Calculating days difference with using angular and jQuery datepicker

查看:19
本文介绍了使用 angular 和 jQuery datepicker 计算天数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 angular 中,我正在尝试使用 jQuery datepicker 计算两个选定日期之间的天数.它只返回我正在使用的函数的 null..

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 一起使用时它可以工作..

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摆弄

here is the working fiddle please check , link to fiddle

创建一个函数来获取传入new 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);
}

这篇关于使用 angular 和 jQuery datepicker 计算天数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆