✍🏼 👨🏫 👨❤️💋👨 How to get date range using moment.js - javascript 🐯 🧘 🤙🏽 ...">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 someone suggest how to do this in my scenario. thanks in advance
user6269870 source shareTo get the remaining dates between two dates, you can refer to 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 a 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", " Tuesday 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 "]
source shareYou can do something like this to get the difference in days: var fromDate = moment(); var toDate = moment().add(15, 'days'); var range = moment().range(fromDate, toDate); var diff = range.diff('days'); var array = range.toArray('days'); $.each(array, function(i, e) { $(".container").append("<li>" + moment(e).format("DD MM YYYY") + "</li>"); }); console.log('Range', range); console.log('diff', diff); EDIT I added a small example for the toArray function: https://jsfiddle.net/Tintin37/19n5ykzs/ source share const getDaysRange = (date1, date2) => { if (date1 && date2) { return Math.abs(moment(date2).diff(moment(date1), 'days')) + 1; } return undefined; }; source shareIf you want to receive days in a row between two dates: - Install MomentJS and MomentRange:
npm i moment && npm i moment-range - Some code
const Moment = require("moment"), MomentRange = require("moment-range"), moment = MomentRange.extendMoment(Moment); let range = moment().range("2018-02-20", "2018-03-01"), array = Array.from(range.by("days")); array.map(m => { console.log(m.format("YYYY-MM-DD")); });
const Moment = require("moment"), MomentRange = require("moment-range"), moment = MomentRange.extendMoment(Moment); let range = moment().range("2018-02-20", "2018-03-01"), array = Array.from(range.by("days")); array.map(m => { console.log(m.format("YYYY-MM-DD")); }); source shareI know this is an old / answer to the question, but since it is at the top of Google, here's how to achieve a date range with only moment (and without moment-range ): import moment from 'moment' /** * @param {date|moment} start The start date * @param {date|moment} end The end date * @param {string} type The range type. eg: 'days', 'hours' etc */ function getRange(startDate, endDate, type) { let fromDate = moment(startDate) let toDate = moment(endDate) let diff = toDate.diff(fromDate, type) let range = [] for (let i = 0; i < diff; i++) { range.push(moment(startDate).add(i, type)) } return range } Returns an array of each "type" (month / day / hour, etc.) between them, excluding the end date Example: let range = getRange('2019-01-01', '2019-01-05', 'days') // moment dates -> [ '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04' ] source share All Articles
|