简体   繁体   中英

Generating Quarterly dates using moment().recur()

I'm building a calendar application where users can save events that recur.

After a lot of research I've chosen to use the moment-recur plugin to the moment.js library to save the dates of recurring events in the database.

My issue is: users in my application will frequently wish to set an event to repeat at the end of a financial quarter.

In the UK these are 31st March, 30th June, 30th September and 31st December.

I can't work out a way to implement this functionality. If I set the recurrence to be '3 months' like this:

let recurring = moment(date).recur().every(3, 'months');

If the begin date is 31/03/2016 then it will skip June altogether as there are only 30 days in June.

If the begin date is 30/06/2016 then it won't work for the quarters that end on the 31st.

Any advice is much appreciated!

you can simply get the current quarter and by keep adding number of quarters like 1,2,3 you will get subsequent quarters.

moment().endOf('quarter').startOf('day');
moment().add(1, 'quarter').endOf('quarter').startOf('day');
moment().add(2, 'quarter').endOf('quarter').startOf('day');
moment().add(3, 'quarter').endOf('quarter').startOf('day');

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