Moment.js Recur Plugin
The Recur plugin is used when one wants to work with recurring dates. This plugin makes it possible to construct length-based intervals (days, weeks, etc.) and calendar-based intervals with this plugin (daysOfMonth, monthsOfYear, etc.).
Write the below command in the terminal to install Recur Plugin:
npm install moment-recur
The following are some of the functions in this plugin:
- recur
- every
- day/days
- week/weeks
- month / months
- year/years
- dayOfWeek/daysOfWeek
- dayOfMonth/daysOfMonth
- weekOfMonth/weeksOfMonth
- weekOfYear/weeksOfYear
- monthOfYear/monthsOfYear
The below examples will help to understand some of the methods of the Recur Plugin.
Example 1:
Javascript
import moment from 'moment' ; import recur from 'moment-recur' ; let arr = [2021,11,31]; let date = moment(arr); let interval = date.recur().every(2).days(); console.log(interval.next(2, 'L' )); |
Output:
Example 2:
Javascript
import moment from 'moment' ; import recur from 'moment-recur' ; let interval=moment().recur({ start: { year:2022, month:1, day:1 }, end: { year:2022, month:11, day:31 }, rules: [ { units: { 2 : true }, measure: "months" } ], exceptions: [{year: 2022,month:5,day:1}] }); console.log( interval.startDate().format( 'dddd, Do MMM YYYY' ) ); console.log( interval.endDate().format( 'dddd, Do MMM YYYY' ) ); console.log(interval.all( 'L' )); |
Output:
Reference: https://momentjs.com/docs/#/plugins/recur/