简体   繁体   English

获取特定日期日历周内的天数

[英]Get day number within calendar week for a specific date

I have a data set which includes dates.我有一个包含日期的数据集。 I need to split this out by week number for reporting purposes.为了报告目的,我需要按周数将其分开。 What I have so far is:到目前为止我所拥有的是:

startDate variable containing 03/01/2015 (populated from data in spreadsheet) startDate 变量包含 03/01/2015(从电子表格中的数据填充)

startDay = Day(startDate)
startMonth = Month(startDate)
startYear = Year(startDate)

startWeek = Application.WorksheetFunction.WeekNum(DateSerial(startYear, startMonth, startDay))

which gives me week 1 in startWeek这给了我 startWeek 的第 1 周

However I know need to know how far into week 1 the date is.但是我知道需要知道日期到第 1 周还有多久。 So for this example, as the date is the 3rd of January, it includes 3 days of week 1所以对于这个例子,因为日期是 1 月 3 日,它包括第 1 周的 3 天

Meaning the reporting I'm putting together will only report on 3 days (as opposed to the full week)这意味着我整理的报告只会报告 3 天(而不是整周)

The only way I've figured to do this so far is to calculate which day of the year the date is and the use a MOD calculation (basically divide by 7 and the remainder is how far into the week it is)到目前为止,我想这样做的唯一方法是计算日期是一年中的哪一天,并使用 MOD 计算(基本上除以 7,余数是它到一周的距离)

dayNumber = DateDiff("d", DateSerial(startYear, 1, 1), DateSerial(startYear, startMonth, startDay)) + 1 

dayOfWeek = dayNumber Mod 7

This does work, but I was wondering if there was a nicer solution than this.这确实有效,但我想知道是否有比这更好的解决方案。

You could use a loop to determine how many days before startDate the week number changed:您可以使用循环来确定周数在startDate之前多少天发生了变化:

Public Sub FindDaysInWeekNo()
    Dim startDate As Date
    startDate = DateSerial(2015, 1, 3)

    Dim startWeek As Integer
    startWeek = Application.WorksheetFunction.WeekNum(startDate)

    Dim i As Integer
    Do While startWeek = Application.WorksheetFunction.WeekNum(DateAdd("d", -i, startDate))
        i = i + 1
    Loop

    Debug.Print i '= 3rd day in this week number
End Sub

The following table shows my comparison to the other suggested formulas and why I think that (refered to =WEEKNUM ) my calculation is correct.下表显示了我与其他建议公式的比较以及为什么我认为( =WEEKNUM )我的计算是正确的。

在此处输入图像描述

Note that if you assume 1st to 7th January will be week 1 (days 1 to 7) you cannot use the WeekNum function because this will give you a different result (see table above and note that the first week has only 6 days according to the WeekNum function).请注意,如果您假设1 月 1 日至 7 日为第 1 周(第 1 至 7 天) ,则不能使用WeekNum函数,因为这会给您不同的结果(请参见上表并注意第一周只有 6 天,根据WeekNum函数)。 Also you cannot name this week number (as what everybody calls week number is defined as https://en.wikipedia.org/wiki/Week#Week_numbering ).您也不能命名本周编号(因为每个人都称周编号定义为https://en.wikipedia.org/wiki/Week#Week_numbering )。

Instead you will need to use …相反,您将需要使用……

Public Function AlternativeWeekNum(startDate As Date) As Integer
    AlternativeWeekNum = WorksheetFunction.Days(startDate, DateSerial(Year(startDate), 1, 1)) \ 7 + 1 'note that this no normal division but a integer division and uses a backslash instead
End Function

to calculate the week number your alternative way, and …以您的替代方式计算周数,以及……

Public Function AlternativeWeekNumDay(startDate As Date) As Integer
    AlternativeWeekNumDay = WorksheetFunction.Days(startDate, DateSerial(Year(startDate), 1, 1)) Mod 7 + 1
End Function

to calculate the day in the alternative week.计算隔周的这一天。

You can use the Weekday() function for this:您可以为此使用Weekday()函数:

=WEEKDAY(B4;2)

The second parameter mentions how you want your days to be counted (starting from Sunday or Monday, counting starting from 0 or from 1, ...).第二个参数提到您希望如何计算您的天数(从星期日或星期一开始,从 0 或 1 开始计数,...)。

dayOfWeek  = (8 + Weekday(startDate) - Weekday(DateSerial(startYear, 1, 1))) mod 7

Just take the positive mod 7 of the difference between the current Day-Of-Week and the Day-Of-Week for the 1st January of whatever the year is只需将当前星期几与每年 1 月 1 日的星期几之间的差取正mod 7

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

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