Issue
This is my html page
<tr ng-show="option == 'Yearly' || option == 'Date'">
<td>
<label>From:</label>
</td>
<td>
<input type="text" ng-model="fromdate" id="from" date-picker date-type="from" month-directive />
{{fromdate}}
</td>
<td>
<label>To:</label>
</td>
<td>
<input id="todate" type="text" ng-model="todate" date-picker date-type="to" month-directive />
{{todate}}
</td>
</tr>
This is my directive
app.directive('monthDirective', function () {
return {
restrict:'A',
link: function (scope, elem) {
var fromDate, toDate;
scope.$watch('fromdate', function (newValue, oldValue) {
fromDate = newValue;
console.log('fromDate', newValue);
});
scope.$watch('todate', function (newValue, oldValue) {
todate = newValue;
console.log('todate', newValue);
});
var range = moment().range(fromDate, toDate);
var diff = moment.preciseDiff(fromDate,toDate);
console.log('Range', range);
console.log('diff', diff);
}
}
})
I need to get the range between fromdate and todate using mument.js. Can anyone suggest how to do this in my scenario. Thanks in advance
Solution
To get rest of the dates between two dates, you can refer this code-
$scope.dateArr = []; //Array where rest of the dates will be stored
$scope.prevDate = moment().subtract(15, 'days');//15 days back date from today(This is the from date)
$scope.nextDate = moment().add(15, 'days');//Date after 15 days from today (This is the end date)
//extracting date from objects in MM-DD-YYYY format
$scope.prevDate = moment($scope.prevDate._d).format('MM-DD-YYYY');
$scope.nextDate = moment($scope.nextDate._d).format('MM-DD-YYYY');
//creating JS date objects
var start = new Date($scope.prevDate);
var end = new Date($scope.nextDate);
//Logic for getting rest of the dates between two dates("FromDate" to "EndDate")
while(start < end){
$scope.dateArr.push(moment(start).format('ddd DD-MM'));
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
}
console.log('Dates:- ');
console.log($scope.dateArr);
This is the console log:-
["Tue 24-05", "Wed 25-05", "Thu 26-05", "Fri 27-05", "Sat 28-05", "Sun 29-05", "Mon 30-05", "Tue 31-05", "Wed 01-06", "Thu 02-06", "Fri 03-06", "Sat 04-06", "Sun 05-06", "Mon 06-06", "Tue 07-06", "Wed 08-06", "Thu 09-06", "Fri 10-06", "Sat 11-06", "Sun 12-06", "Mon 13-06", "Tue 14-06", "Wed 15-06", "Thu 16-06", "Fri 17-06", "Sat 18-06", "Sun 19-06", "Mon 20-06", "Tue 21-06", "Wed 22-06"]
Answered By - sajalsuraj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.