[英]How do I convert a calendar week into a date in Excel?
I have a week number and a year, and would like to calculate the date of the Monday for that specific week in Microsoft Excel.我有一个周数和一年,并且想在 Microsoft Excel 中计算该特定周的星期一的日期。
Year Week Date (Monday)
2012 1 January 2, 2012
2013 16 April 15, 2013
2014 42 October 13, 2014
What formula can I use to convert a calendar week to a specific date?我可以使用什么公式将日历周转换为特定日期?
For ISO week numbers you can use this formula to get the Monday对于 ISO 周数,您可以使用此公式获得星期一
=DATE(A2,1,-2)-WEEKDAY(DATE(A2,1,3))+B2*7
assuming year in A2 and week number in B2假设 A2 中的年份和 B2 中的周数
it's the same as my answer here https://stackoverflow.com/a/10855872/1124287这与我在这里的回答相同https://stackoverflow.com/a/10855872/1124287
The following formula is suitable for every year.以下公式适用于每一年。 You don't need to adjust it anymore.
你不需要再调整它了。 The precondition is that Monday is your first day of the week.
前提是星期一是您一周的第一天。
If A2 = Year and Week = B2
=IF(ISOWEEKNUM(DATE($A$2;1;1)-WEEKDAY(DATE($A$2;1;1);2)+1)>1;DATE($A$2;1;1)-WEEKDAY(DATE($A$2;1;1);2)+1+B2*7;DATE($A$2;1;1)-WEEKDAY(DATE($A$2;1;1);2)-6+B2*7)
If your week number is in A1 and the year is in A2, following snippet could give you dates of full week如果您的周数在 A1 中,年份在 A2 中,则以下代码段可以为您提供整周的日期
=$A$1*7+DATE($B$1,1,-4) through =$A$1*7+DATE($B$1,1,2)
Of course complete the series from -4 to 2 and you'll have dates starting Sunday through Saturday.当然,完成从 -4 到 2 的系列,您将有从周日到周六开始的约会。
Hope this helps.希望这可以帮助。
如果周数在A1
,年份在A2
,您可以尝试:
A1*7+DATE(A2,1,1)
A simple solution is to do this formula:一个简单的解决方案是做这个公式:
A1*7+DATE(A2,1,1)
If it returns a Wednesday, simply change the formula to:如果它返回星期三,只需将公式更改为:
(A1*7+DATE(A2,1,1))-2
This will only work for dates within one calendar year.这仅适用于一个日历年内的日期。
If A1 has the week number and year as a 3 or 4 digit integer in the format wwYY then the formula would be:如果 A1 的周数和年份为 wwYY 格式的 3 位或 4 位整数,则公式为:
=INT(A1/100)*7+DATE(MOD([A1,100),1,1)-WEEKDAY(DATE(MOD(A1,100),1,1))-5
the subtraction of the weekday ensures you return a consistent start day of the week.减去工作日可确保您返回一周的一致开始日。 Use the final subtraction to adjust the start day.
使用最后的减法来调整开始日期。
=(MOD(R[-1]C-1,100)*7+DATE(INT(R[-1]C/100+2000),1,1)-2) =(MOD(R[-1]C-1,100)*7+DATE(INT(R[-1]C/100+2000),1,1)-2)
yyww as the given week exp:week 51 year 2014 will be 1451 yyww 作为给定的周 exp:week 51 year 2014 将是 1451
If you are looking for the opposite to get a date from a weeknumber i found a solution online and changed it slightly:如果您正在寻找相反的方法来从周数中获取日期,我在网上找到了一个解决方案并对其稍作更改:
Function fnDateFromWeek(iYear As Integer, iWeek As Integer, iWeekDday As Integer)
' get the date from a certain day in a certain week in a certain year
fnDateFromWeek = DateSerial(iYear, 1, (iWeek * 7) _
+ iWeekDday - Weekday(DateSerial(iYear, 1, 1)) + 1)
End Function
I took the formular from asap-utilities.com/ and changed我从 asap-utilities.com/ 获取了公式并进行了更改
DateSerial(iYear, 1, ((iWeek - 1) * 7)
to到
DateSerial(iYear, 1, (iWeek * 7)
It seems that at least for germany the formular works not as expected for the leap year 2016 so i changed it to this似乎至少对德国而言, 2016 年闰年的公式没有按预期工作,所以我将其更改为
Function fnDateFromWeek(iYear As Integer, iWeek As Integer, iWeekDday As Integer)
' get the date from a certain day in a certain week in a certain year
If isLeapYear(iYear) Then
curDate = DateSerial(iYear, 1, ((iWeek) * 7) _
+ iWeekDday - Weekday(DateSerial(iYear, 1, 1)) + 1)
Else
curDate = DateSerial(iYear, 1, ((iWeek - 1) * 7) _
+ iWeekDday - Weekday(DateSerial(iYear, 1, 1)) + 1)
End If
fnDateFromWeek = curDate
End Function
Since 2016 hardcoded is not ideal you could check if a year is a leap year with this function由于 2016 年硬编码并不理想,您可以使用此功能检查一年是否为闰年
Function isLeapYear(iYear As Integer) As Boolean
If (Month(DateSerial(iYear, 2, 29)) = 2) Then
isLeapYear = True
Else
isLeapYear = False
End If
End Function
For a non-leap-year DateSerial(iYear,2 ,29)
returns 1st of march对于非闰年
DateSerial(iYear,2 ,29)
返回 3 DateSerial(iYear,2 ,29)
1 日
This may still be wrong but my limited test gave the expected results:这可能仍然是错误的,但我有限的测试给出了预期的结果:
Sub TestExample()
Debug.Print Format(fnDateFromWeek(2014, 48, 2), "ddd dd mmm yyyy") ' mo 24 Nov 2014
Debug.Print Format(fnDateFromWeek(2015, 11, 6), "ddd dd-mmm-yyyy") ' fr 13 Mar 2015
Debug.Print Format(fnDateFromWeek(2016, 36, 2), "ddd dd-mmm-yyyy") ' Mo 05 Sep 2015
End Sub
I'm sorry but none of the solutions I could understand actually gave the correct result. 抱歉,我能理解的解决方案均未给出正确的结果。 In some instances the start of the week was after the actual date.
在某些情况下,一周的开始是在实际日期之后。 ie the start date of the week for a Sunday was the coming Monday.
即,星期日的一周的开始日期是下一个星期一。 Start of week error
周初错误
I would appreciate if someone can correct my thinking. 如果有人可以纠正我的想法,我将不胜感激。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.