简体   繁体   中英

Monthly recurring event on specific date

I want to create a recurring date that repeats monthly

For eg:

A user select a date “2022-09-16”

the ouput should be:

[“2022-09-16”, ”2022-10-16”,”2022-11-16”,”2022-12-16”]

I have no idea how to implement using dayjs and dayjs-recur

Any help would be appreciated

You can initiate a date by the starting day. After that iterate 3 times, and every time add one month to it then push a new string to the resulting array.

 const getDates = str => { const dates = [str]; const date = new Date(str); for(let i = 0; i < 3; i++) { date.setMonth(date.getMonth() + 1); dates.push(date.toISOString().substring(0, 10)); } return dates; } console.log( getDates('2022-09-16') );

I solved it

dayjs().recur(dayjs().add(12, "month")).every(1, "month")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM