简体   繁体   English

计算日期范围在两个日期之间发生的天数

[英]Calculate the number of days of a date range occur between two dates

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) 我需要计算圣诞节假期(12月11日至1月7日)的开始日期和结束日期之间有多少天

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. 因此,例如,当开始日期是2012年12月12日,结束日期是2013年12月1日时,这些日期之间有28天。 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. 当开始日期为2012年12月15日,结束日期为2013年3月3日时,则这些日期之间的间隔为22天。 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). 如果开始日期是2012年12月12日,结束日期是2014年12月1日,则这些日期之间有56天(因为该范围为两年)。

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. 最初,我决定使用圣诞节(12月25日)发生的次数,只计算每次发生4周。

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. 您在第二个示例中的日期数学是错误的-该范围内有24天,而不是22天。

I can get you there for one holiday period: 我可以带你去一个假期:

LET: 让:

  • A1 contain the holiday start (11-Dec-2012) A1包含假期开始时间(2012年12月11日)
  • A2 contain the holiday end (7-Jan-2013) A2包含假期结束时间(2013年1月7日)
  • B1 contain the start date B1包含开始日期
  • B2 contain the end date B2包含结束日期

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. “ +1”是因为您的“结束日期”实际上是包含在内的,因此对于日期数学,您需要使假日在1月8日而不是1月7日结束,以捕获最后一天。

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 此公式将计算12月11日至1月7日之间的日期(包括任意年份),甚至跨多年

=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 您的开始日期在B1中,结束日期在B2中

This converts every date in the range to a number , eg 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 这会将范围内的每个日期转换为数字,例如12月1日变为1201,3月4日变为304,然后对那些大于或等于1207(12月7日)或小于或等于107(1月7日)的日期进行计数,因此最后一个示例将为56

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, ie 如果您从每个日期中减去7(以1月7日为结束日期),则可以缩短公式,则只需检查结果数字> = 1204,即

=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 .....第三个选择也应产生相同的结果,更接近理查德塔伦的方法-获取年数并乘以28,然后根据开始/结束日期调整数字

=(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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM