Calculating dates in a year by a start and end date

olive2403

New Member
Joined
Feb 15, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone,

I am trying to calculate the number of days a year someone was in a location using a start and end date. Any idea how to do this.

Example:

Period Start DatePeriod End Date
7/20/2020​
12/31/2022​

So in this case, I need to know the number of days in 2020, 2021 and 2022.

Thanks!!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi,

Does something like this work for you:

Book3.xlsx
ABCDEFGHI
1Period Start DatePeriod End DateDays20192020202120222023
27/20/202012/31/2022 164365364 
311/30/20198/15/202331366365365226
Sheet1000
Cell Formulas
RangeFormula
E2:I3E2=IF(OR(YEAR($A2)>E$1,E$1>YEAR($B2)),"",IF(YEAR($B2)=E$1,DAYS($B2,DATE(E$1,1,1)),DAYS(DATE(E$1,12,31),$A2)-SUM($D2:D2)))
 
Upvote 0
If you need the End Date Inclusive

Use this instead:

Book3.xlsx
ABCDEFGHI
1Period Start DatePeriod End DateDays20192020202120222023
27/20/202012/31/2022 164365365 
311/30/20198/15/202331366365365227
Sheet1000
Cell Formulas
RangeFormula
E2:I3E2=IF(OR(YEAR($A2)>E$1,E$1>YEAR($B2)),"",IF(YEAR($B2)=E$1,DAYS($B2,DATE(E$1,1,1))+1,DAYS(DATE(E$1,12,31),$A2)-SUM($D2:D2)))
 
Upvote 0
Welcome to the Forum!

Another way, using Excel 365 functionality:

ABCDEFGH
1StartEnd20192020202120222023
220 Jul 202031 Dec 2022-165365365-
330 Nov 201915 Aug 202332366365365227
41 Jan 202131 Dec 2022--365365-
531 Dec 20191 Jan 202113661--
61 Jan 20221 Jan 2022---1-
Sheet4
Cell Formulas
RangeFormula
D1:H1D1=LET(y,YEAR(MIN(A2:A6)),SEQUENCE(,1+YEAR(MAX(B2:B6))-y,y))
D2:H6D2=LET(s,DATE(D$1#,1,1),x,IF(EDATE(s,12)>B2,1+B2,EDATE(s,12))-IF(s<A2,A2,s),IF(x>0,x,"-"))
Dynamic array formulas.
 
Upvote 0
Hi,

Does something like this work for you:

Book3.xlsx
ABCDEFGHI
1Period Start DatePeriod End DateDays20192020202120222023
27/20/202012/31/2022 164365364 
311/30/20198/15/202331366365365226
Sheet1000
Cell Formulas
RangeFormula
E2:I3E2=IF(OR(YEAR($A2)>E$1,E$1>YEAR($B2)),"",IF(YEAR($B2)=E$1,DAYS($B2,DATE(E$1,1,1)),DAYS(DATE(E$1,12,31),$A2)-SUM($D2:D2)))
Thank you! Do you happen to know why this would work until 2021? I'm going from 2014-2021 and any date range that starts and ends in 2021, is coming back with 365.
1644985166635.png
 
Upvote 0
If you need the End Date Inclusive

Use this instead:

Book3.xlsx
ABCDEFGHI
1Period Start DatePeriod End DateDays20192020202120222023
27/20/202012/31/2022 164365365 
311/30/20198/15/202331366365365227
Sheet1000
Cell Formulas
RangeFormula
E2:I3E2=IF(OR(YEAR($A2)>E$1,E$1>YEAR($B2)),"",IF(YEAR($B2)=E$1,DAYS($B2,DATE(E$1,1,1))+1,DAYS(DATE(E$1,12,31),$A2)-SUM($D2:D2)))
Actually i'm noticing its wrong for any start or end dates that happen within the same year. Do you happen to know? For example a start of 8/4/2014 and 8/14/2014, is resulting in 226 days in 2014.
 
Upvote 0
Welcome to the Forum!

Another way, using Excel 365 functionality:

ABCDEFGH
1StartEnd20192020202120222023
220 Jul 202031 Dec 2022-165365365-
330 Nov 201915 Aug 202332366365365227
41 Jan 202131 Dec 2022--365365-
531 Dec 20191 Jan 202113661--
61 Jan 20221 Jan 2022---1-
Sheet4
Cell Formulas
RangeFormula
D1:H1D1=LET(y,YEAR(MIN(A2:A6)),SEQUENCE(,1+YEAR(MAX(B2:B6))-y,y))
D2:H6D2=LET(s,DATE(D$1#,1,1),x,IF(EDATE(s,12)>B2,1+B2,EDATE(s,12))-IF(s<A2,A2,s),IF(x>0,x,"-"))
Dynamic array formulas.
Thank you but unfortunately I have Excel 16 and so this appears to be erroring.
 
Upvote 0
Thank you but unfortunately I have Excel 16 ...
Oops, sorry. Don't know how I managed to read 2016 as 365 ...

Try this instead:

ABCDEFGH
1StartEnd20192020202120222023
220 Jul 202031 Dec 2022-165365365-
330 Nov 201915 Aug 202332366365365227
41 Jan 202131 Dec 2022--365365-
531 Dec 20191 Jan 202113661--
61 Jan 20221 Jan 2022---1-
Sheet1
Cell Formulas
RangeFormula
D2:H6D2=IFERROR(1/(1/MAX(0,MIN(DATE(D$1+1,1,1),$B2+1)-MAX($A2,DATE(D$1,1,1)))),"-")
 
Upvote 0
Actually i'm noticing its wrong for any start or end dates that happen within the same year. Do you happen to know? For example a start of 8/4/2014 and 8/14/2014, is resulting in 226 days in 2014.

Fixed below:

Book3.xlsx
ABCDEFGHI
1Period Start DatePeriod End DateDays20192020202120222023
27/20/202012/31/2022 164365365 
311/30/20198/15/202331366365365227
48/4/20228/14/2022   10 
Sheet1000
Cell Formulas
RangeFormula
E2:I4E2=IF(OR(YEAR($A2)>E$1,E$1>YEAR($B2)),"",IF(YEAR($B2)=E$1,IF(YEAR($A2)=E$1,DAYS($B2,$A2),DAYS($B2,DATE(E$1,1,1))+1),DAYS(DATE(E$1,12,31),$A2)-SUM($D2:D2)))
 
Upvote 0

Forum statistics

Threads
1,214,390
Messages
6,119,235
Members
448,879
Latest member
VanGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top