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
To get rest of the dates between two dates, you can refer this code-
This is the console log:-
Whilst not as fun as coming up with your own function, the easy answer is to use https://github.com/rotaready/moment-range
If you want to get consecutive days between two dates:
npm i moment && npm i moment-range
You can do something like this to get the difference in days:
EDIT
I added a little example for the
toArray
function :https://jsfiddle.net/Tintin37/19n5ykzs/
I know this is an old/answered question, but since it's at the top of google here is how to achieve a date range with only
moment
(and withoutmoment-range
):Returns an array of every "type" (month/day/hour etc) between the two, exclusive of the end date
Example: