Calculate the number of days of a date range occur between two dates - excel

I have a datasheet that contains a list of start and end dates for a task. I need to calculate how many days between the start date and end date are part of our Christmas break (11th December to 7th January)
So for example,when start date is 10/12/2012 and end date is 12/01/2013, 28 of the days are between those dates. when the start date is 15/12/2012 and the end date is 12/03/2013, then 22 days of days are between those dates. If the start date is 10/12/2012 and the end date is 12/01/2014, 56 of the days are between those dates (because there's two years of the range).
I need to do this with a formula because of the requirements that I've been set.
Initially I decided to use the number of times Christmas Day (25th December) occurs and just calculate 4 weeks per occurrence.
The formula I used was
=FLOOR((E12-A25)/365,1)+IF(OR(MONTH(E12)=12,
MONTH(A25)=12),
IF(AND(DAY(A25)<=25,DAY(E12)>=25),1,0),
IF(OR(MONTH(A25)>=12,
IF(MONTH(E12)<MONTH(A25),
MONTH(E12)+12,
MONTH(E12))>=12),1,0))*28
But obviously this doesn't help if the range start and end date falls between those two dates.
Any suggestions? I'm at a dead end

Your date math on the second example is wrong -- there are 24 days in that range, not 22.
I can get you there for one holiday period:
LET:
A1 contain the holiday start (11-Dec-2012)
A2 contain the holiday end (7-Jan-2013)
B1 contain the start date
B2 contain the end date
FORMULA:
=MAX(MIN(A2+1,B2+1),A1) - MIN(MAX(A1,B1),A2+1)
The formula basically finds the overlapping date range, if there is one, and subtracts to get the number of whole days. The "+1" is because your "end dates" are actually inclusive, so for date math you need to have the holiday ending on 8 Jan, not 7 Jan, to capture that last day.
But this only works for a single year's holiday. You could store holiday ranges in other cells and use the same formula and add them all up, but you'll be limited to however many years you set up.

This formula will count dates between 11th December and 7th January inclusive within any date range, even across multiple years
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0>=1211)+(TEXT(ROW(INDIRECT(B1&":"&B2)),"mmdd")+0<=107))
where your start date is in B1 and end date in B2
This converts every date in the range to a number , e.g. 1st Dec becomes 1201, 4th March becomes 304, then it counts those dates that are either greater or equal to 1207 (7th December) or smaller than or equal to 107 (7th January), so that will give 56 for your last example
You can shorten the formula if you subtract 7 from every date (based on 7th January as the end date) then you only need to check that resultant numbers are >= 1204, i.e.
=SUMPRODUCT((TEXT(ROW(INDIRECT(B1-7&":"&B2-7)),"mmdd")+0>=1204)+0)
.....and a third option which should also give the same result, closer to richardtallent's approach - gets the number of years and multiplies by 28 and then adjusts the figures based on the start/end date
=(YEAR(B1-7)-YEAR(B2-7)+1)*28-MAX(0,B2-DATE(YEAR(B2-7),12,11))-MIN(28,DATE(YEAR(B1-7)+1,1,7)-B1)

Related

Find first date after today that matches a day-number and weekday

Given any date, how do I find the first month and year in the future where the 31st of that month falls on a Wednesday, in other words, for today, 11 November 2020, I would like to see (31) March 2021. [Updated: Returned month should be March 2021]
#prino,
You could make use of Excel's Iterative Calculation, setting Maximum Iterations to 1000.
In the example below, A1 is the given date and B1 receives the result (put the formula in B1):
=IF(B1=0,A1)+B1+IF(AND(WEEKDAY(B1,1)=4,DAY(B1)=31),0,1)
If you have Excel 365 then
=LET(DayInMonth, DATE(YEAR($A$2),MONTH($A$2)+SEQUENCE(336,,0,1),$B$2),
DOW, WEEKDAY(DayInMonth),
IsMatch, (DAY(DayInMonth)=$B$2)*(DOW=$C$2)*(DayInMonth>=$A$2),
XLOOKUP(1,IsMatch,DayInMonth,,0,1))
How it works:
Parameters are on the sheet (you could hard code these into the formula if you wish)
A2 is the start date (could be formula =TODAY())
B2 is the day number in the month
C2 is the day of week code (1..7 = Sunday..Saturday)
The calander repeats every 28 years. So only need to consider 12*28 = 336 months
DayInMonth is the specified date in each of the next 336 months, including this month. Note: if a month does not have the specified day (eg 31 September does'nt exist)
DOW is the day of Week code for those dates
IsMatch is 1 if calculated DayInMonth matches specified DayInMonth (this excluded non-existant dates) AND Calculated DOW matches specified DOW AND calculated date is on or after start date
XLOOKUP returns the date of the first 1 in the IsMatch array

How to collect number of days from a month in excel

I have a column which will have some days from a month in dd-mm-yyyy format. What I need to get total number of days from each month mentioned in that column. The column might not contain all days from each month.
Example :
Column A :
2017-01-01
2017-01-02
2017-01-17
2017-01-27
2017-02-08
In above example, from 2017 Jan, I have 4 days and from Feb I have one day. I need this count for a IF statement to run in C column. ie, in C1 there will be an IF function which will check the month in A1 and count the total number of days for that specific month from A column. If that count is below a specific digit, I need to perform a formula otherwise a different one. This will repeat for all C cell
If this column contains all date from each month I can use Day function to get total days, but here how Can I collect it
This will work: =SUMPRODUCT(1*(MONTH($A1:$A5)=D1))
And an example of manipulating basing on the result:
=IF(SUMPRODUCT(1*(MONTH($A1:$A5)=D1))<3;"good";"bad")
Another one when row number represents a month (JAN in C1, FEB in C2...):
=IF(SUMPRODUCT(1*(MONTH($A1:$A5)=ROW()))<3;"good";"bad")
In your case:
=IF(SUMPRODUCT(1*(MONTH($A1:$A5)=ROW()))<10;function1();function2())
Array formula - use Ctrl+Shift+Enter:
=SUM((MONTH($A$1:$A$5)=MONTH(A1))*(YEAR($A$1:$A$5)=YEAR(A1)))

How to calculate the number of weeks between 2 date range in excel

I have a question here, I have 3 columns in excel, A1 is being the calendar date; column B1 has the fiscal_quarter_start_date ; and fiscal_quarter_end_date in column C1.
I am trying to find the number of week falling in between the A2 and A3. The issue here is I should consider the first sunday as the first week of the quarter. My fiscal year starts on Feb of every month.
The reason I am doing this calculation is to populate the count of weeks numbers of a quarter, where my first week starts from the first sunday of the quarter.
=ROUNDUP(MOD(IF(A13>=IF(DATE(YEAR($A13),2,1)=1,DATE(YEAR($A13),2,1),DATE(YEAR(A13),2,7-WEEKDAY(DATE(YEAR(A13),2,1),1)+2)),ROUNDUP((A13-IF(WEEKDAY(DATE(YEAR(A13),2,1),1)=1,DATE(YEAR($A13),2,1),DATE(YEAR($A13),2,7-WEEKDAY(DATE(YEAR($A13),2,1),1)+2))+1)/7,0),ROUNDUP((A13-IF(DATE(YEAR($A13)-1,2,1)=1,DATE(YEAR($A13)-1,2,1),DATE(YEAR($A13)-1,2,7-WEEKDAY(DATE(YEAR($A13)-1,2,1),1)+2))+1)/7,0)),13.01),0)
above is the formula i've got all the way. However rounding up with 13 is not a viable solution as its not necessary that it should have always have 13 weeks all the time.
For example: 5/1/16 should be week 1 As it is a begging of a q2 and 5/1/16 is the starting on Sunday. In this case using above formula result for the week is 13
Please help me..
You can use WEEKNUM to get the week number.
However you mentioned that your week wont end at the Saturday, but at the Sunday, so you have to do =WEEKNUM(<insert here where the date is>,2)
=WEEKNUM (serial_num, [return_type])
serial_num - A valid Excel date in serial number format.
return_type - [optional] The day the week begins. Default is 1.
Return_type can either be 1 or 2:
1 - Week begins on Sunday
2 - Week begins on Monday
or... read it here for counting weeks between 2 dates.
Enter this formula =(A2-A1)/7 into a blank cell, (A2 indicates the end date and A1 is the start date)
If you just want to get the whole weeks, please apply this INT function: =INT((A2-A1)/7)
Try it here, on sheet "week"
Hope it'll help.
Assuming that the Quarter start and end dates are in A3:A6 respectively then:
=IF(AND(C1>=$A3,C1<$A4),WEEKNUM(C1)-WEEKNUM($A3)+1,IF(AND(C1>=$A4,C1<$A5),WEEKNUM(C1)-WEEKNUM($A4)+1,IF(AND(C1>=$A5,C1<$A6),WEEKNUM(C1)-WEEKNUM($A5)+1,WEEKNUM(C1)-WEEKNUM($A6)+(IF(AND(C1>=DATE(YEAR(C1),1,1),C1<DATE(YEAR(C1),MONTH($A6),DAY($A6))),53,1)))))
This is checking whether the date in the cell above (C1) is between the quarter start and end dates, and checking the difference in weeks from the start date. To cover the anomoly for Q4 extending into the next year, the IF statement at the end adds 53 instead of 1 for the affected dates.
Edit #1
=WEEKNUM(A1)-WEEKNUM(B1)+IF(AND(A1>=DATE(YEAR(A1),1,1),A1<DATE(YEAR(A1),MONTH(B1),DAY(B1))),53,1)
This works for the A1:C1 format you mentioned in your comment. Obviously this relies on B1 and C1 being updated manually.
Edit #2
=WEEKNUM(C1)-WEEKNUM(B1)
If you are counting 05/01/2016 as week 1 then add 1 to the result. Take off the highest week number (default is counting from sunday or you can change to (C1,1) if you want to define it exactly) from the lowest week number. Reult is 13 which would match the manual calculation.
You will want to look at the previous edit for an example of the if formula you will need when the quarter ends in another year. to reverse the negative figure it will need to add 52 when the date is in the early part of the next year and 0 when it is not.
You can use the "hidden" function DATEDIF:
For example in A1 we write start date (01/05/2016), in B1 - end date (31/07/2016) and in C1 = DATEDIF(A1,B1,"d")/7 the result will be 13

Find last digits with IF

I have lots of cells in the spreadsheet that contains the whole year, date and stock prices, but I only need the dates from 25th of the last month till the 5th of this month.How can I write a function to do that? For example if the cells are like this in the column:
19900101
19900102
19900103
19900104
19900105
.
.
19900125
19900126
19900127
19900128
19900129
19900130
19900131
I want it to show only days (1,2,3,4,5,25,26,27,28,29,30,31) with the date format.
The date shows in the text format and I've tried format cell to change it but it doesn't work.
For a slightly different approach, assuming you want to return the A2 value only if it represents a date in the last 5 or first 5 days of any month you can use this formula in B2
=IF(DAY(TEXT(A2,"0000-00-00")+5)<11,A2,"")
TEXT function converts your data to a real date then if you add 5 days to that date then any date that was originally in the last 5 or first 5 of the month will now be in the first 10 days of the month.....so it's sufficient to check that the day is < 11. That will pick up 27th to 31st of a 31 day month, 26th to 30th of a 30 day month etc.
you could use a formula and then filter by rows with data...
As an example (given it's difficult to discern if you always want the first 5 and last 5 of a month or what....)
=IF(RIGHT(A2,2)*1<=2,A2,IF(RIGHT(A2,2)*1>=29,A2,""))
Based on all the edits and comments, these should be the function you need.
=IF(OR(A7-A2>5,MOD(A2,100)<=5),DATE(LEFT(A2,4),MID(A2,5,2),RIGHT(A2,2)),"")
You should place it in cell B2 and then do all the desired filtering, as explained in xQbert's answer.

In Excel, how to calculate # of days between a date range, counting only specific days of week

I'm trying to get # days between 2 dates, but had to count only days of the week specified per pair of dates in questions.
For example:
Date of operation = 9/1/2013 and 5/16/2014
Days of the Week in operation = Monday, Wednesday, Friday
I want to calculate total number of days in actual operation, given the days in operation per week.
If you have Excel 2010 or later you can use NETWORKDAYS.INTL function which allows you to customise which days are counted, e.g. the following formula will count Mondays, Wednesdays and Fridays between a start date in A2 and an end date in B2 (inclusive) while excluding holiday dates listed in H2:H10
=NETWORKDAYS.INTL(A2,B2,"0101011",H$2:H$10)
The string "0101011" defines the days to include - 0 is included, 1 is excluded - it starts with Monday
In earlier excel versions you can use this formula for the same result
=SUMPRODUCT((WEEKDAY(ROW(INDIRECT(A2&":"&B2)))={2,4,6})*(COUNTIF(H$2:H$10,ROW(INDIRECT(A2&":"&B2)))=0))
where {2,4,6} defines the weekdays to include (1=Sun through to 7=Sat)
The number of days between to dates is just date1 - date2.
One method is to divide the number of days in operation by the number of days in the week. In this case that would be 3/7. In this example, the number of days in operation would be 110.14 days. Below is your example. cell B4 has 1-sep-13, b6 has 16-may-14, both formatted as dates.
B
4 41518
6 41775
8 =B6-B4 Days
9 =B8/7 Weeks
10 =B8*3/7
01-Sep-13
16-May-14
257 Days
36.71 Weeks
110.14
To get more accurate, you would need to know which day of the week, weekday(), the first day in your range was and which day of the week your last day was.

Resources