-1

pls check this my problem is similar to this How to manage date using momentjs for a monthly payments? The result what I want in Monthly is like this if the given date is 01-25-2022 and the terms is 3

Result
02-25-2022 
03-25-2022
04-25-2022

semi-monthly if the given date is 04-29-2022 and the terms is 3

Result
05-14-2022
05-29-2022
06-14-2022

<!--FOR MONTHLY -->


var dateRelease = new Date("01-25-2022")

const terms = 3
for (let i = 0; i < terms; i++) {
  console.log(new Date(dateRelease.setDate(dateRelease.getDate() + 31)).toISOString().slice(0, 10))

}

<!--FOR SEMI-MONTHLY -->


var dateRelease = new Date("04-29-2022")

const terms = 3
for (let i = 0; i < terms; i++) {
  console.log(new Date(dateRelease.setDate(dateRelease.getDate() + 15)).toISOString().slice(0, 10))

}
Not a Pro
  • 163
  • 2
  • 12
  • I am not able to understand what do you exactly want ? – Sumit Sharma May 25 '22 at 09:38
  • ohh sorry my problem is similar to this https://stackoverflow.com/questions/68903953/how-to-manage-date-using-momentjs-for-a-monthly-payments – Not a Pro May 25 '22 at 09:43
  • You can not just add 31 or 15 days, the result will of course depend on how many days the starting month has to begin with. If you just want to advance a given date by "a month" in the colloquial sense, then you should be manipulating the month portion of the Date, not add 31 days. And your second case will probably require that you check how many days the current month has first. – CBroe May 25 '22 at 09:44
  • Yes you're right, I have no idea how to do that :( – Not a Pro May 25 '22 at 09:46

1 Answers1

1

Moment.js is made for this job.

<!--FOR MONTHLY -->

var dateRelease = moment("2022-01-25")

const terms = 3
for (let i = 0; i < terms; i++) {
  console.log(dateRelease.add(1, 'months').format('YYYY-MM-DD'))

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

<!--FOR SEMI-MONTHLY -->


var dateRelease = moment("2022-04-29")

const terms = 3
for (let i = 0; i < terms; i++) {
  console.log(dateRelease.add(2, 'weeks').format('YYYY-MM-DD'))

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
  • Monthly is good but how about the semi-monthly ? How can I dipslay out put like this `05-14-2022 05-29-2022 06-14-2022` – Not a Pro May 25 '22 at 09:50
  • Semi-monthly usually is 2 weeks... And 2 weeks are 14 days on planet earth. That is what Moment gives here. But if you really want 15 days, you can do `.add(15, 'days')`. – Louys Patrice Bessette May 25 '22 at 09:58
  • Okay let say this is not a semi-monthly but how ca I get the this result if the given date is `04-29-2022` the return date is like this `05-14-2022` `05-29-2022` `06-14-2022` – Not a Pro May 26 '22 at 02:37