Exclude February 29th from the time interval

I want to exclude February 29 from my calculations of the absence of full days between two dates. Excluding angular interval values.

Testing Scenarios:

1 March 2012 - 28th February 2012  = should give 0 days.
1 March 2012 - 26th February 2012  = should give 2 days.
1 March 2013 - 28th February 2012  = should give 365 days and not 366 days.
1 March 2017 - 28th February 2012  = should give 365*5 =1825 days and not 1827 days.

I am currently looking at the year and checking to see if there is any leap year between dates, and then check if the interval is February 29th. Is there a way to understand if a particular interval has a specific day and the number of cases if the interval does not exceed several years.

+5
source share
4 answers

but it is not caused by the 29th, it is correct.

if you compare March 1, 2012 and February 28, 2012, 1 day will return, and from the 29th day 2 days will return. The difference between the two dates is one day.

0 , , , , 1 2012 1 2012 .

, , ^^.

+1

Java-esque . , :

int daysExcludingLeapYearDay(Date start, Date end)
{
    if(end.isBefore(start))
    {
        return filterOutLeapYearDays(daysBetween(end, start)).size();
    }

    return filterOutLeapYearDays(daysBetween(start, end)).size();
}

List<Date> filterOutLeapYearDays(List<Date> days)
{
    List<Date> daysExcludingLeapYearDays = 
             days
             .filter(d -> !isFeb29(d))
             .toList());

    return daysExcludingLeapYearDays;
}

List<Date> daysBetween(Date start, Date end) 
{
    List<Date> days = new List();

    for (Date day = start; day.isBefore(end); day = day.plusOneDay()) 
    {
        days.add(day);
    }

    return days
}

boolean isFeb29(Date date) 
{
    return date.month == FEBRUARY && date.day == 29;
}
+1

360- , , 365- .

29 , .

, , 29 , .

, JodaTime,

  • 1 1970 .
  • 1 1970 .
  • .
  • .
  • 29 , 1
  • , .
0

Using javascript, you can get it in the following way, where startDateand endDateare the javascript objects Date.

function calculateDays(startDate,endDate){
    var oneDay = 86400000; //ms per day
    var difference = Math.ceil((endDate.getTime() - startDate.getTime()) / oneDay); 
    var totFeb29s = findTotalFeb29s(startDate,endDate);
    return  parseInt(difference)+parseInt(1)-parseInt(totFeb29s);    
}    
function findTotalFeb29s(startDate,endDate){
    var totalFeb29s = 0;
    var startYear = startDate.getFullYear();
    var endYear = endDate.getFullYear();
    for(var year=startYear;year<=endYear;year++){
        var dt = new Date(year, 1, 29);
        var isLeap = dt.getMonth() == 1;
        if(isLeap==true && startDate<=dt && dt<=endDate){
           totalFeb29s = parseInt(totalFeb29s)+parseInt(1);
        }
    }
    return totalFeb29s;
}    
0
source

All Articles