If you are still interested, here is further analysis of the question posed:
We consider a guest's stay somewhere and our objective is to determine how many nights of the stay occurred during the month associated with an arbitrary System Date, S. The guest's stay is described in terms of an Arrival Date, A, and a Departure Date, D.
Logical Constraints:
Time is resolved to the day, so generally A<=D, otherwise a "time's arrow error" (TAE) is introduced because arrivals must precede departures. The special case where A=D is considered valid because of the time resolution limit.
Since the objective depends on the months associated with the dates, the beginning of month (bm) and end of month (em) associated with each date may be important, and nomenclature is adopted such that bm or em is prefixed to the date variable, e.g., emS is the end-of-month date associated with S. For any part of the guest's stay to occur during the month represented by S, both of these constraints must be true: S>=bmA AND S<=emD.
- If the first were not true, then S would occur in some month prior to the month when the guest arrives, and no part of the guest's stay would occur during the month associated with S. So the answer is 0 due to S<bmA.
- If the second were not true, then S would occur in some month after the month when the guest departs, and no part of the guest's stay would occur during the month associated with S. So the answer is 0 due to S>emD.
Analysis:
There are six ways to arrange three dates in sequence: {ADS; ASD; SAD; SDA; DAS; DSA}. The latter three are not valid due to a TAE. Each of the first three valid sequences can overlay a monthly calendar in multiple ways. Many of those arrangements result in 0 for the "number of days stayed in S month", NDS, due to the logical constraints described above. For those cases where the logical constraints do not result in 0, the following expression establishes the objective quantity:
(D-A)-IF(D>emS,(D-emS)-1,0)-IF(A<bmS,bmS-A,0)
- The (D-A) term represents the number of day intervals between the two dates, and thus already considers that the night of the departure day does not count as a night stayed, so no correction term is needed.
- The (D-emS) term represents the number of day intervals between the departure date and the end of the System Date month, yet the last night of the System Date month would count as a night stayed, therefore this difference is 1 day too large and a correction term of -1 is needed.
- The (bmS-A) term does not require this same correction because the night leading into bmS is assigned to the day bmS-1, thus the number of day intervals is correct.
Assembling all of this into a single formula: NDS = IF(A>D,"TAE",IF(OR(S<bmA,S>emD),0,(D-A)-IF(D>emS,D-emS-1,0)-IF(A<bmS,bmS-A,0)))
Excel formulae for bm and em quantities in expression above:
bmA=EOMONTH(A,-1)+1
bmS=EOMONTH(S,-1)+1
emD=EOMONTH(D,0)
emS=EOMONTH(S,0)
Substitution to express in terms of Excel functions and date variables:
NDS=IF(A>D,"TAE",IF(OR(S<(EOMONTH(A,-1)+1),S>EOMONTH(D,0)),0,(D-A)-IF(D>EOMONTH(S,0),D-EOMONTH(S,0)-1,0)-IF(A<(EOMONTH(S,-1)+1),(EOMONTH(S,-1)+1)-A,0)))
MrExcel_2.xlsm |
---|
|
---|
| A | B | C | D | E | F |
---|
1 | System Date (mm/dd/yyyy) | Arrival (mm/dd/yyyy) | Departure (mm/dd/yyyy) | Nights Stayed in month of System Date | Total Nights Stayed | Notes about terms invoked in formula |
---|
2 | 01/26/2020 | 01/23/2020 | 02/05/2020 | 9 | 13 | |
---|
3 | 01/31/2020 | 01/28/2020 | 02/05/2020 | 4 | 8 | D>emS |
---|
4 | 01/31/2020 | 01/28/2020 | 01/27/2020 | TAE | TAE | time's arrow error |
---|
5 | 01/15/2020 | 02/13/2020 | 02/17/2020 | 0 | 4 | S<bmA |
---|
6 | 03/15/2020 | 02/13/2020 | 02/17/2020 | 0 | 4 | S>emD |
---|
7 | 01/18/2020 | 01/15/2020 | 01/19/2020 | 4 | 4 | simple (D-A) |
---|
8 | 01/18/2020 | 01/28/2020 | 02/05/2020 | 4 | 8 | D>emS |
---|
9 | 02/03/2020 | 01/28/2020 | 02/05/2020 | 4 | 8 | A<bmS |
---|
10 | 01/01/2020 | 12/29/2019 | 01/01/2020 | 0 | 3 | |
---|
11 | 01/12/2020 | 12/31/2019 | 01/05/2020 | 4 | 5 | |
---|
12 | 01/28/2020 | 12/25/2019 | 01/10/2020 | 9 | 16 | |
---|
|
---|