【问题标题】:Given a 4-5-4 calendar and a date, how do I determine what fiscal week that date falls in?给定 4-5-4 日历和日期,我如何确定该日期属于哪个会计周?
【发布时间】:2015-06-16 19:39:09
【问题描述】:

我们有一个 4-5-4 日历,其中财政年度从 2 月的一个星期日开始。对于 2016 财年,第一天实际上是在 1 月——即 1 月 31 日星期日。

我需要编写一个函数,将日期作为输入并返回财政周,例如 201552,这将是 2015 财政年度的第 52 个财政周。

我认为第一步是确定输入日期的会计年度的开始日期。我知道它总是星期天,但我怎么知道它是日历二月的第一个星期日,还是日历一月的最后一个星期日?

(幸运的是,出于此功能的目的,我可以忽略偶尔的第 53 周;在这种情况下,我可以返回第 52 周。那是因为(我被告知)具有 53 个会计周的年份是不可预测的(无论如何在这家公司)并且是由人的突发奇想决定的。)

有什么建议吗?

更新:

我收到了一份包含我们公司从 2005 财年到 2017 财年的财年日历的文件。我看到的模式是:

  • 如果 2 月 1 日是星期一、星期二或星期三,则 FY 的第一天是 1 月的最后一个星期日。
  • 如果 2 月 1 日是星期四、星期五、星期六或星期日,则 FY 的第一天是 2 月的第一个星期日。

我认为这给了我我需要的东西。

【问题讨论】:

  • 什么语言?到目前为止,您尝试了哪些代码?
  • 我正在使用 Excel VBA。到目前为止,我所拥有的只是一个将日期作为输入的函数,并为该输入设置年份、月份数和月份中的日期的变量。这还不是尝试代码的问题。首先我需要弄清楚要尝试什么。
  • @GregLovern 请编辑您的问题以澄清和添加细节,而不是作为 cmets 发布。此外,在问题上添加一种编程语言作为标签。 Stack Overflow 中的自动代码格式化工具使用该标签。
  • 试试weeknum() (:
  • @ p._phidot_,Excel 的 WEEKNUM() 工作表函数不知道 4-5-4 日历。首先,您必须在 return_type 参数中提供开始日期,因此您必须有另一个函数来确定那一天是什么,这比简单的工作 WEEKNUM 更难。然后,您必须减去 4,因为 WEEKNUM 认为 1 月的第一周是第 1 周。或者至少您通常必须减去 4;我不确定有时是 3 还是 5。到今年年底,你必须添加,什么,通常是 48?使用 WEEKNUM 只会让工作变得更难。

标签: excel date calendar vba


【解决方案1】:

我认为第一步是找到输入日期所在年份的 2 月 1 日的星期几。

接下来,根据一周中的 2 月 1 日查找 FY 的第一天。如果是周一、周二或周三,则 FY 的第一天是 1 月的最后一个周日。否则,FY 的第一天是 1 月的第一个星期日。

接下来,判断输入日期是在那个 FY 还是之前的那个。

那么,如果输入的日期是上一财年,则获取该财年的第一天。

接下来,计算从 FY 的第一天到输入日期的天数。除以 7,向上舍入到下一个整数。那是财政年度。

此时我会知道输入日期的FY年和FY周,并可以返回。

更新:

这就是我所拥有的;它适用于我的测试:

Public Function ConvertDateToRawFYWeek(convert_date As Date) As String
'Fiscal year starts on either:
    'the last Sunday in January (if Feb 1st is a Mon, Tue, or Wed), OR:
    'the first Sunday in February (if Feb 1st is Thur, Fri, Sat, or Sun).

    Dim iCalendarYearOfInputDate As Long, iInputMonth As Long, iInputDay As Long, iTmpYear As Long
    Dim iFebFirstOfTmpYear As Long, strFebFirstWeekdayOfTmpYear As String
    Dim iFirstDayofFYOfTmpYear As Long
    Dim iFiscalYearOfInputDate As Long
    Dim iDayOfInputDate As Long
    Dim iDayOfFY As Long, iWeekOfFY As Long, strWeekOfFY As String
    Dim bDone As Boolean

    iCalendarYearOfInputDate = Year(convert_date)
    iInputMonth = Month(convert_date)
    iInputDay = Day(convert_date)
    iDayOfInputDate = CLng(DateValue(convert_date))


    bDone = False 'init.
    iTmpYear = iCalendarYearOfInputDate 'init.
    Do

            '***get the day of the week of feb 1st of tmp date's year:
            iFebFirstOfTmpYear = DateSerial(iTmpYear, 2, 1)
            strFebFirstWeekdayOfTmpYear = Format(iFebFirstOfTmpYear, "DDDD")

            '***get the first day of the FY of the tmp date's year:
            Select Case strFebFirstWeekdayOfTmpYear
                Case "Monday"
                    'first day of the tmp year's FY is the last Sunday of January, which for the tmp year is Jan 31st:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear - 1
                Case "Tuesday"
                    'first day of the tmp year's FY is the last Sunday of January, which for the tmp year is Jan 30th:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear - 2
                Case "Wednesday"
                    'first day of the tmp year's FY is the last Sunday of January, which for the tmp year is Jan 29th:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear - 3
                Case "Thursday"
                    'first day of the tmp year's FY is the first Sunday of February, which for the tmp year is Feb 4th:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear + 3
                Case "Friday"
                    'first day of the tmp year's FY is the first Sunday of February, which for the tmp year is Feb 3rd:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear + 2
                Case "Saturday"
                    'first day of the tmp year's FY is the first Sunday of February, which for the tmp year is Feb 2nd:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear + 1
                Case "Sunday"
                    'first day of the tmp year's FY is the first Sunday of February, which for the tmp year is Feb 1st:
                    iFirstDayofFYOfTmpYear = iFebFirstOfTmpYear
            End Select

            '***get the fiscal year of the input date:
            If iDayOfInputDate >= iFirstDayofFYOfTmpYear Then
                iFiscalYearOfInputDate = iTmpYear
                bDone = True
            Else
                iTmpYear = iTmpYear - 1 'loop again.
            End If

    Loop Until bDone


    '***count the days from that first day of the FY, to the day of the input date.
    'Divide by 7, rounding UP to the next integer. That is the FY week.
    iDayOfFY = iDayOfInputDate - iFirstDayofFYOfTmpYear
    iWeekOfFY = Round((iDayOfFY / 7) + 0.50000000000001) 'round up to next integer.
    strWeekOfFY = Format(iWeekOfFY, "00")

    strFY = Format(iTmpYear, "0000")

    ConvertDateToRawFYWeek = strFY & strWeekOfFY

End Function

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2022-01-24
    相关资源
    最近更新 更多