Forum Discussion

rreiser68's avatar
rreiser68
Copper Contributor
Dec 06, 2021

calculate date difference

I am looking for a formula to calculate the difference between two dates, while controlling for the year.  I have a series of pairs of dates (A1,B1) (A2,B2)...  The first date always precedes, or is the same as, the second date chronologically.  The dates may occur before, during or after the year 2021.  I am looking for a formula that will calculate the number of days that occur between the pairs, and only occur in 2021.  I like the DATEDIF function, but will use whatever works.

5 Replies

  • Riny_van_Eekelen's avatar
    Riny_van_Eekelen
    Platinum Contributor

    rreiser68 Perhaps this is what you need to calculate only the number of days that fall in 2021.

    =MAX(0,MIN(B1,DATE(2021,12,31))-MAX(A1,DATE(2021,1,1))+1)

     

    • rreiser68's avatar
      rreiser68
      Copper Contributor
      Riny_van_Eekelen
      This is VERY helpful. However, I don't quite understand why there is a +1 at then end of the formula. Could you possibly explain it to me?
      • mtarler's avatar
        mtarler
        Silver Contributor
        I believe the +1 is to include the start/end dates. So 2021-01-05 - 2021-01-03 would result in 2 (basically 5-3=2) but you actually have the 3rd, 4th, and 5th so the +1 is needed.
  • mtarler's avatar
    mtarler
    Silver Contributor
    the raw representation for dates is in days so basically just subtract the 2 dates. I don't understand the "while controlling the year" part.

Resources