A Moment.js plugin for getting the weekdays of a month, year, or inbetween two dates.
Since Moment.js is being deprecated, it only makes sense to deprecate this project as well. Feel free to fork and use this code in any way you wish however I will no longer be maintaining it.
- moment.js v2.1.0 or higher
npm install --save moment-weekdaysin
var moment = require('moment-weekdaysin');
moment().weekdaysInMonth('Monday');
<script src="moment.js"></script>
<script src="moment-weekdaysin.js"></script>
<script>
moment().weekdaysInYear('Friday');
</script>
bower install --save moment-weekdaysin
// All weekends in the summer of 2010
var summerWeekends = moment('2010-06-20').weekdaysInBetween('2010-09-23', [0, 5, 6]);
// Mondays remaining this year
var remainingMondays = moment().weekdaysInBetween(moment().endOf('year'), 'Monday');
// Last Wednesday
var lastWednesday = moment().dayOfYear(1).weekdaysInBetween(null, 3, -1);
// Second to last Friday before Christmas
var paydayBeforeChristmas = moment().weekdaysInBetween(moment().month('December').date(25), 'Friday', -2);
// Holidays this year
var memorialDay = moment().month('May').weekdaysInMonth('Monday', -1);
var laborDay = moment().month('September').weekdaysInMonth('Monday', 1);
var thanksgivingDay = moment().month('November').weekdaysInMonth('Thursday', 4);
// Wednesdays in May of 1995
var mayWednesdays = moment('1999-05-01').weekdaysInMonth(3);
// Last two weekends of the year
var lastWeekendsOfYear = moment().weekdaysInYear([0, 5, 6], [-1, -2]);
// Thursdays this year
var thursdaysThisYear = moment().weekdaysInYear('Thursday');
// The 10th and 4th to last Sundays and Wednesdays this year
var complicatedExample = moment().weekdaysInYear(['Sunday', 3], [10, -4]);
- date (
weekdaysInBetween
only) - The end date forweekdaysInBetween
. Can be any ISO date string that moment accepts or a moment object. Defaults to today. - weekdays - The weekdays to fetch. Can be any value that moment().day() accepts or an array of those values. Defaults to all.
- index - The index of the days to fetch. 1-indexed based. Can be a negative number.
1
will get the first instance,-1
will get the last. Can be a single value or an array of values. Defaults to all.
All parameters are optional.
All three functions will:
- Return a sorted array of moment objects if there are multiple found.
- Return a single moment object if there is only one found.
- Return false if none are found.
- Never return duplicate moment objects.
That means something like this may be necessary:
var t = moment().weekdaysInBetween(moment().endOf('year'), 'Monday');
if (t) {
if (t.length) {
// Returned multiple
} else {
// Returned one
}
} else {
// Returned none
}
However if weekdays
and index
parameters are both set to single values, you will never get multiple values returned.
MIT. See the License file for more info.