function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Karthik Sundara RajKarthik Sundara Raj 

Try to find Feb 29's between two randam Dates?

       Hi All,
I would like to find how many feb 29's between two randam dates.I found a solution and it working but some test cases dose not working.
        integer n =0;
        integer Sdate=2012;
        integer Edate1=Sdate;
        integer Edate=Edate1+2;//2014
        integer smonth=2;
        integer sday=2;
        integer Emonth=2;
        integer Eday=1;

        For(Integer i =0 ; i<=Edate- Sdate; i++){
             If (Math.mod(Sdate, 400) == 0 ||( Math.mod(Sdate, 4) == 0 && Math.mod(Sdate, 100) != 0)&&(Edate == Sdate)){
                n=n+1;
                Sdate=Sdate+1;
                i--;
                //system.debug('Counts of (n) = '+ n);
            }
            else{
                If (Math.mod(Sdate, 400) == 0 ||( Math.mod(Sdate, 4) == 0 && Math.mod(Sdate, 100) != 0)){
                    n=n+1;
                    Sdate=Sdate+1;
                    i--;
                }
                Else{
                    n=n+0;
                    Sdate=Sdate+1;
                    i--;
                }
            }
        }
        system.debug('Start Year = '+ Sdate);
        system.debug('End Year = '+ Edate);
           system.debug('Counts of (n) = '+ n);

Thanks,

Karthik
Best Answer chosen by Karthik Sundara Raj
mritzimritzi
I have written an Apex class which you can execute and verify
 
public class TestClass2 { 
  
  Date dt1;
  Date dt2;
  public TestClass2(){
	// change date values as per your choice
    dt1 = date.newinstance(2014,01,01);
	dt2 = date.newinstance(2016,04,29);
  }
  
  public void getCount(){
      Integer i,count=0;
      for(i=dt1.year();i<=dt2.year();i++){
		/*
			29th feb would be counted only 
			if start date(dt1)is less than 29 feb of start year
			i.e 28th feb of any year
			and end date (dt2) is more than 28th feb of end year
		*/
		// i sets year value
          Date tmpDt = date.newinstance(i,2,28);
          if(tmpDt>=dt1 && tmpDt<dt2){
              if(date.isLeapYear(tmpDt.year()))
                  count++;
          }
      }
      System.debug(count);
      
  }
}


Select it as Best Answer, it it solves your problem
 

All Answers

mritzimritzi
I have written an Apex class which you can execute and verify
 
public class TestClass2 { 
  
  Date dt1;
  Date dt2;
  public TestClass2(){
	// change date values as per your choice
    dt1 = date.newinstance(2014,01,01);
	dt2 = date.newinstance(2016,04,29);
  }
  
  public void getCount(){
      Integer i,count=0;
      for(i=dt1.year();i<=dt2.year();i++){
		/*
			29th feb would be counted only 
			if start date(dt1)is less than 29 feb of start year
			i.e 28th feb of any year
			and end date (dt2) is more than 28th feb of end year
		*/
		// i sets year value
          Date tmpDt = date.newinstance(i,2,28);
          if(tmpDt>=dt1 && tmpDt<dt2){
              if(date.isLeapYear(tmpDt.year()))
                  count++;
          }
      }
      System.debug(count);
      
  }
}


Select it as Best Answer, it it solves your problem
 
This was selected as the best answer
Karthik Sundara RajKarthik Sundara Raj
Sorry for delayed reply Mritzi and thanks a lot for your help.