简体   繁体   English

如何从ISO日期和星期获取月份

[英]How to get Month from ISO Date and Week

I'm looking for a way to convert an ISO date to Month and I also need to covert an ISO week to Month. 我正在寻找一种将ISO日期转换为Month的方法,并且还需要将ISO Week转换为Month。 I need to do this in Excel and Access. 我需要在Excel和Access中执行此操作。 I found this formula for excel but when converting it to Access it does not work. 我发现此公式适用于excel,但将其转换为Access时不起作用。 Is there a simple way to accomplish this? 有没有简单的方法可以做到这一点? I found this Excel formula to convert ISO date to month (C2=date) and it works perfect: 我发现这个Excel公式可以将ISO日期转换为月份(C2=date) ,并且可以完美地工作:

=MONTH(DATE(YEAR(C2),MONTH(C2)+(WEEKDAY(C2,2)+(DAY(DATE(YEAR(C2),MONTH(C2)+1,0)))-(DAY(C2))<4),(((7-(WEEKDAY(C2,2)))+(DAY(C2)))>3)))

But when I modify it for an Access query it does not return the correct values: 但是,当我为Access查询修改它时,它不会返回正确的值:

Date_to_Month:MONTH(DATESERIAL(YEAR([WW_Index].[ISO_date]),MONTH([WW_Index].[ISO_date])+(WEEKDAY([WW_Index].[ISO_date],2)+(DAY(DATESERIAL(YEAR([WW_Index].[ISO_date]),MONTH([WW_Index].[ISO_date])+1,0)))-(DAY([WW_Index].[ISO_date]))<4),(((7-(WEEKDAY([WW_Index].[ISO_date],2)))+(DAY([WW_Index].[ISO_date])))>3)))

I also need to convert ISO week_year to Month. 我还需要将ISO week_year转换为Month。 I found this formula but it does not work: 我找到了这个公式,但它不起作用:

=MONTH(DATE(YEAR(C2),1,-2)-WEEKDAY(DATE(YEAR(C2),1,3))+D2*7)

Example: week 18, 2012 is the ISO week of Apr 30 through May 6, 2012. There is less that 4 April days in this week thus week 18, 2012 is in May. 例如:2012年第18周是2012年4月30日至2012年5月6日的ISO周。本周少于4月4日,因此2012年第18周是5月。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I don't know access but you can do the first part (date to month) much more easily in excel with this formula 我不知道访问权限,但是您可以使用此公式在excel中更轻松地完成第一部分(日期至月份)

=MONTH(C2-WEEKDAY(C2-1)+4)

That should be easier to convert for access...... 那应该更容易转换以便访问......

For the second part you are finding the Monday of the relevant week, hence you get the wrong month in some cases, the Thursday of the week (midpoint) should always be within the correct month so you can just add 3 to get that (-2 becomes 1), ie 在第二部分中,您找到了相关星期的星期一 ,因此在某些情况下您会输入错误的月份, 星期四 (中点)应始终在正确的月份之内,因此您只需加3就可以了(- 2变成1),即

=MONTH(DATE(YEAR(C2),1,1)-WEEKDAY(DATE(YEAR(C2),1,3))+D2*7)

I assume C2 is a date within the relevant year and D2 is the ISO week number but probably better to have C2 containing the year, eg just 2013 and then you can use 我假设C2是相关年份内的日期 ,D2是ISO周号,但最好包含C2包含年份,例如2013年,然后就可以使用

=MONTH(DATE(C2,1,1)-WEEKDAY(DATE(C2,1,3))+D2*7)

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

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