简体   繁体   English

如何计算 EXCEL 中的年份、星期几和星期几的日期?

[英]how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

在此处输入图像描述

I have the Year, Week of Year and Day of the Week and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture我有年、年中的星期和星期中的日期,我想将日期估计为 dd.mm.yyyy,它以黄色突出显示,如 EXCEL 图片中所示

I tried many formulas, but I am sure there might be an easy one.我尝试了很多公式,但我相信可能有一个简单的公式。

I found the solution:我找到了解决方案:

Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)

=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6

I found this solution, but you need to do further testing if it really works.我找到了这个解决方案,但如果它真的有效,你需要做进一步的测试。

I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)我从一周开始计算月份:=+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)我根据星期名称计算星期数: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)

And then make date using: =DATE(A2;C2;E2)然后使用:=DATE(A2;C2;E2)

在此处输入图像描述

I think you are counting the weeks starting from zero because for 9/1/2022 ( YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)) .我认为您是从零开始计算周数,因为对于9/1/2022YYYY/MM/DD格式),根据 function WEEKNUM(DATE(2022,9,1))的结果,相应的周为36 In order to use the logic to multiply the number of weeks by 7 .为了使用逻辑将周数乘以7 You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week.你需要使用一年的第一天作为参考,如果它是一个星期天,如果不是那么 go 回到上一个星期日,所以你可以计算整个星期。 Bottom line use as a reference date , the Sunday of the first week of the year, not the first day of the year ( YYYY/1/1 )底线用作参考日期,一年中第一周的星期日,而不是一年中的第一天 ( YYYY/1/1 )

Here is the approach we use in cell E2 :这是我们在单元格E2中使用的方法:

=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
 fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk 
  + XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))

We use the LET function to avoid repeating the same calculation.我们使用LET function 来避免重复相同的计算。 The following expression finds the previous Sunday if the first day of the year ( fDay ) was not a Sunday:如果一年的第一天 ( fDay ) 不是星期日,则以下表达式查找前一个星期日:

fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))

The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. XLOOKUP function 用于获取工作日的数字表示,并使用TEXT function 生成长格式的工作日。 Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1 .由于我们计算整个星期,如果工作日是星期日(我的屏幕截图中的C列),那么我们不需要在参考日期中添加任何一天,这就是我们使用seq-1的原因。

Here is the output for several sample data.这是几个示例数据的 output。 Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.假设周计数从零开始,如果不是,则需要调整公式以及输入数据。

excel输出

Notice that the year 2021 started on a Friday, so if we want to find a day for the first week ( 0 ) before Friday it will return a date from the previous year.请注意,2021 年是从星期五开始的,因此如果我们想在星期五之前的第一周 ( 0 ) 找到一天,它将返回上一年的日期。 Like in the case of Monday.就像周一的情况一样。 If you want an error message instead, then the formula can be modified as follow:如果您想要一条错误消息,则可以按如下方式修改公式:

=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
 result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
  + XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
 IF(YEAR(result) <> y, "ERROR: Date from previous year", result))

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

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