Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

angularjs - Calculating days difference with using angular and jQuery datepicker

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..

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..

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/

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

here is the working fiddle please check , link to fiddle

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;
}

modify the dayDiff function as below,

$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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...