Populate To Date based on From Date in AngularJS

mahesh213
 
on Apr 02, 2020 12:12 AM
1207 Views

Hi,

How to populate Todate Value based upon From Date

I have 2 date fileds From date and Todate

ex: I have selected from date as 02-04-2020 then automatically populate Todate as '01-04-2021'

Display validation if date is greater than N-Days from Current (Today) Date on Button Click in AngularJS

could you please help me

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 02, 2020 01:16 AM
on Apr 03, 2020 07:43 AM

Hi mahesh213,

First you need to add 1 year to the selected date and then substract 1 day from the updated date to get to date.

Refer below code.

HTML

<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', []);
        app.directive('datepicker', function () {
            return {
                restrict: 'A',
                // Always use along with an ng-model
                require: '?ngModel',

                link: function (scope, element, attrs, ngModel) {
                    if (!ngModel) return;

                    ngModel.$render = function () {
                        element.datepicker('update', ngModel.$viewValue || '');
                    };

                    element.datepicker().on("changeDate", function (event) {
                        scope.$apply(function () {
                            ngModel.$setViewValue(event.date);
                            var fromDate = new Date(event.date);
                            var toDate = new Date(fromDate.setFullYear(fromDate.getFullYear() + 1));
                            toDate = new Date(toDate.setDate(toDate.getDate() - 1));
                            toDate = (toDate.getMonth() + 1) + "/" + toDate.getDate() + "/" + toDate.getFullYear();
                            scope.toDate = toDate;
                        });
                    });
                }
            };
        });
        app.controller('MyController', function ($scope) {
            var firstDay = (new Date().getMonth() + 1) + "/01/" + new Date().getFullYear();
            $scope.fromDate = firstDay;
        });
    </script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    From Date : <input datepicker ng-model="fromDate" /><br />
    To Date : <input datepicker ng-model="toDate" />
</body>
</html>

Demo