Home > database >  How to calculate last date of last 5 financial years. if current date is greater or lesser than 31st
How to calculate last date of last 5 financial years. if current date is greater or lesser than 31st

Time:10-22

if current date is greater than 31st march then the output should be (31st march 2021,31st march 2020,31st march 2019,31st march 2018,31st march 2017).if current date is lesser than 31st march then the output should be (31st march 2020,31st march 2019,31st march 2018,31st march 2017,31st march 2016)

CodePudding user response:

The method will get the current date & will compare with the current year march,31 dates. If the current date is greater than Mar,31 then it will add the next 5-year date of 31-March. If the current date is less than Mar,31 then it will add the previous 5-year date of 31-March.

 function getYearList(){
   var yearList = [];
   var currentDate=new Date();
   //new Date(year, month, day, hours, minutes, seconds, milliseconds)
   // Here 00=Jan,01=Feb,02=Mar etc.
   var comparingDate=new Date(currentDate.getFullYear(),02,31);
   var yearCount=currentDate.getFullYear();
   if(currentDate>=comparingDate){ 
        for (let i = 0; i <= 4; i  ) {    
            var newDate=new Date((yearCount i),02,31);
            yearList.push(newDate);
         }
  }else{
      for (let i = 4; i >= 0; i--) {      
         var newDate=new Date((yearCount-i),02,31);          
          yearList.push(newDate);
         }
      }
   console.log(yearList);
 }

CodePudding user response:

PeriodChange(event: any) {
this.YearMap=[];
this.currentYear = (new Date()).getFullYear();
this.selectedYears = 5;
this.SelectedCertificate = Number(this.selectedYears);
const comparisondate = "March 31 "   this.currentYear;
const requiredDate = new Date(comparisondate);

requiredDate < new Date() ? this.increment=0 : this.increment=1;
for (let i = 0; i < Number(this.CACertificateselectedYears); i  ) {
  this.YearMap.push("31 March " (this.currentYear-this.increment));
  this.currentYear-=this.increment;
  console.log(this.YearMap);
  this.increment === 1 ?this.increment :this.increment  ;      
}

}

CodePudding user response:

You can get the start year based on whether the date is before or after 31 March, then get the 5 prior instances of that date.

The function can be made more general by allowing the user to specify the number of dates to get while defaulting to 5. E.g.

// Get previous n dates for 31 March from date
function getYears(date = new Date(), n = 5) {
  let year = date.getFullYear();
  year -= date < new Date(year, 3, 1)? 1 : 0;
  return new Array(n).fill().map(() => new Date(year--, 2, 31));
}

// Examples
console.log('Start today');
getYears().forEach(d=>console.log(d.toDateString()));
console.log('\nStart 1 Jan 2020');
getYears(new Date(2020,0)).forEach(d=>console.log(d.toDateString()));
console.log('\nGet 10 years, start today');
getYears(new Date(), 10).forEach(d=>console.log(d.toDateString()));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related