Add 12 months from selected date in javascript c#

durga valliammai 21 Reputation points
2022-08-11T14:32:53.227+00:00

I have a gridview called gvFiscalPeriod. I have a textbox where user can enter a date for example '1/11/2022' (start date). Based on the date, I should be able to generate for next 12 months, where each row is a month with start date and end date.

Eg:
id | StartDate | EndDate
1 1/11/2022 30/11/2022
2 1/12/2022 31/12/2022
3 1/1/2023 31/01/2023

This is my script :

function SaveAddFiscalPeriod() {
gvDT.row.add([
ID,
$(StartDate).val(),
$(EndDate).val()

            ]).draw(false);    

            gvDT.columns.adjust().draw();    
        }    
    });    

But this script is only adding one row , but I want to add 12 rows with startdate and enddate. Kindly help me.

Microsoft 365 and Office Development Office JavaScript API
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2022-08-11T14:47:25.69+00:00

    Do that using a loop that iterates 12 times. (Not tested code)

       //Given the start date, add a row for 12 months from that day  
       function SaveAddFiscalPeriod( startDate ) {  
         
          //Convert to a date type - assuming first of month otherwise you'll need to adjust the day to 1  
          var start = Date.parse(startDate);  
         
          //Note that setMonth modifies `start` so it jumps 1 month each time through the loop  
          for (let index = 0; index < 12; ++index) {  
              gvDT.row.add([ ID, start, new Date(start.setMonth(date.getMonth()+1)) ]);  
          }  
       }  
    

    In your current code you're inserting a row that has an ID, start and end date. I don't know how that maps to what you're asking so I'm assuming the start date is the beginning of the month and the end date is the end of the month. I'm not sure what you're $(EndDate) element points to but you said given a start date add a row for each month so end date is sort of implied.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.