sudarsan
sudarsan

Reputation: 75

How to find exact month and days between two dates - angularjs

I want to calculate the month and days between two dates. Then need to calculate the cost to be paid by the user between the start date and end date.

Cost = 10000.00; Periodicity = 3 (Quaterly);

No of months and days = (Start Date - End Date);

Payment Need to be paid = ((No of months and days / Periodicity) * Cost);

I try to find the time difference between two dates but it is not working properly for me. Can anyone help me?

var date = "2017-06-01";
            console.log("date: "+date);
            var currentDate = $filter('date')(date, "yyyy-MM-dd HH:mm:ss");

            $scope.userdob = "2017-08-31";
            var dobdate = $filter('date')($scope.userdob, "yyyy-MM-dd HH:mm:ss");

            console.log("dob: "+dobdate);

            /* differentiate Date */            
            var date1 = $filter('date')($scope.userdob, "yyyy-MM-dd");
            var date2 = $filter('date')(date, "yyyy-MM-dd");

            date1 = date1.split('-');
            date2 = date2.split('-');

            // Now we convert the array to a Date object, which has several  helpful methods
            date1 = new Date(date1[0], date1[1], date1[2]);
            date2 = new Date(date2[0], date2[1], date2[2]);

            if (date1 < date2)
            {
                var start_date = date1;
                var end_date = date2;
                var inverse = false;

                end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects.
                end_date.setDate(end_date.getDate() + 1);

                // Calculate the differences between the start and end dates
                var yearsDifference = end_date.getFullYear() - start_date.getFullYear();

                var monthsDifference = end_date.getMonth() - start_date.getMonth();

                var daysDifference = end_date.getDate() - start_date.getDate();

                var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0);
                var noOfDays = d1.getDate();

                $scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + daysDifference/noOfDays); // Add fractional month 

Upvotes: 1

Views: 1566

Answers (3)

Nazim Khan
Nazim Khan

Reputation: 11

function findDateDifferenceInDays(sdate,edate) {
    var oneDay = 24*60*60*1000;
    var firstDate = new Date(sdate);
    var secondDate = new Date(edate);

    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
}

Upvotes: 1

sudarsan
sudarsan

Reputation: 75

This solves my problem.

$scope.startDate = new Date(startDate);

                $scope.endDate = new Date(endDate);

                console.log($scope.startDate);
                console.log($scope.endDate);

                /* differentiate Date */            
                var date1 = $filter('date')($scope.startDate, "yyyy-MM-dd");
                var date2 = $filter('date')($scope.endDate, "yyyy-MM-dd");

                console.log("date1 : "+date1);
                console.log("date2 : "+date2);

                date1 = date1.split('-');
                date2 = date2.split('-');

                // Now we convert the array to a Date object, which has several helpful methods
                /*date1 = new Date(date1[0], date1[1], date1[2]);
                date2 = new Date(date2[0], date2[1], date2[2]);*/

                console.log("date1 : "+date1);
                console.log("date2 : "+date2);
                if ($scope.startDate < $scope.endDate)
                {
                    var start_date = date1;
                    var end_date = date2;
                    var inverse = false;
                    console.log("end_date : "+end_date);

                    end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects.
                   console.log("end_date : "+end_date); end_date.setDate(end_date.getDate() + 1);

                    start_date = new Date(start_date);


                    // Calculate the differences between the start and end dates
                    var yearsDifference = end_date.getFullYear() - start_date.getFullYear();

                    var monthsDifference = end_date.getMonth() - start_date.getMonth();

                    var daysDifference = end_date.getDate() - start_date.getDate();

                    var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0);
                    var noOfDays = d1.getDate();

                    if(noOfDays >= 30) {
                        noOfDays = 30;
                    }

                    var days = daysDifference/noOfDays;

                    console.log(days);

                    $scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + days); // Add fractional month

                    console.log($scope.noOfMonths);

Upvotes: 0

Horia Coman
Horia Coman

Reputation: 8781

Date manipulation is notoriously tricky to get right. There's leap years, leap seconds, and other various forms of time adjustments that happen. For a big, but nowhere near exhaustive list, checkout Falsehoods programmers believe about time and More falsehoods programmers believe about time. Your best bet when doing something nontrivial with dates and times is to use a good library, such as moment. It won't get everything right, of course, but it will do a good enough job for most purposes.

It can handle parsing for you as well, so the previous code can become something like this:

const date1 = moment($filter('date')($scope.userdob, "yyyy-MM-dd"));
const date2 = moment($filter('date')(date, "yyyy-MM-dd"));

const diffInMonths = date1.diff(date1, 'months');
const diffInDays = date1.diff(date2, 'days');

Upvotes: 0

Related Questions