Number of days in a year that fell between 2 dates

User avatar
Abraxus
3StarLounger
Posts: 263
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Number of days in a year that fell between 2 dates

Post by Abraxus »

I've been pulling my hair out on this one and just can't seem to wrap my head around the necessary logic.

I have 2 dates and a year. 1/1/23 and 4/4/27 and 2024, for example.

I need to know how many days in 2024 fall between those 2 dates.

In this example it's 366, but if they year were 2027, it would only be 95.

Any pointers on how to make this function?

Code: Select all

Function GetNumberOfDays(dteOne as Date, dteTwo as Date, strYear as String) as Integer
   dim intToReturn as Integer
   NO IDEA WHAT GOES HERE...
GetNumberOfDays = intToReturn
End Function
Morgan

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Number of days in a year that fell between 2 dates

Post by HansV »

Shouldn't it be 94 days in 2027?
31 in January + 28 in February + 31 in March + 4 in April = 94.

Code: Select all

Function GetNumberOfDays(ByVal dteOne As Date, ByVal dteTwo As Date, ByVal lngYear As Long) As Long
    Dim lngToReturn As Long
    If dteOne < DateSerial(lngYear, 1, 1) Then
       dteOne = DateSerial(lngYear, 1, 1)
    End If
    If dteTwo > DateSerial(lngYear, 12, 31) Then
        dteTwo = DateSerial(lngYear, 12, 31)
    End If
    lngToReturn = dteTwo - dteOne + 1
    If lngToReturn < 0 Then
        lngToReturn = 0
    End If
    GetNumberOfDays = lngToReturn
End Function
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 263
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Number of days in a year that fell between 2 dates

Post by Abraxus »

HansV wrote:
01 Nov 2023, 20:17

31 in January + 28 in February + 31 in March + 4 in April = 94.
I had written down the number of days using 2024, which is a leap year, and forgot to change it for 2027 which is not. :-)

Thank you!
Morgan

User avatar
p45cal
2StarLounger
Posts: 166
Joined: 11 Jun 2012, 20:37

Re: Number of days in a year that fell between 2 dates

Post by p45cal »

In the attached, in column C, a plain formula:
=MAX(0,MIN(DATE(A5+1,1,0),$B$2)-MAX($A$2,DATE(A5,1,1))+1)
in column D, HansV's function
in column E, a lambda function (DaysOverlap) based on the plain formula in column C
2023-11-02_013843.jpg
You do not have the required permissions to view the files attached to this post.

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: Number of days in a year that fell between 2 dates

Post by snb »

Alternative UDF

In F5: =F_snb($A$2:$B$2;$A5)

Code: Select all

Function F_snb(sn, n)
  For j = sn(1, 1) To sn(1, 2)
    If Year(j) = n Then F_snb = F_snb + 1
  Next
End Function

User avatar
SpeakEasy
5StarLounger
Posts: 611
Joined: 27 Jun 2021, 10:46

Re: Number of days in a year that fell between 2 dates

Post by SpeakEasy »

We can shorten that a little more :wink: :

Code: Select all

Function F_snb(sn, n)
  For j = sn(1, 1) To sn(1, 2)
    F_snb = F_snb - (year(j) = n)
  Next
End Function

snb
5StarLounger
Posts: 613
Joined: 14 Nov 2012, 16:06

Re: Number of days in a year that fell between 2 dates

Post by snb »

No objection, your honour. :clapping: