简体   繁体   English

给定4-5-4日历和日期,我如何确定该日期属于哪个会计周?

[英]Given a 4-5-4 calendar and a date, how do I determine what fiscal week that date falls in?

We have a 4-5-4 calendar in which the fiscal year starts on a Sunday in February. 我们有一个4-5-4日历,其中会计年度从2月的星期日开始。 For FY 2016, that first day is actually in January -- it's Sunday, January 31st. 对于2016财年,第一天实际上是在一月-它是1月31日星期日。

I need to write a function that takes a date as an input and returns the fiscal week, such as 201552, which would be the 52nd fiscal week of fiscal year 2015. 我需要编写一个将日期作为输入并返回会计周的函数,例如201552,这将是2015会计年度的第52个会计周。

I think the first step is figuring out the starting day of the input date's fiscal year. 我认为第一步是确定输入日期会计年度的开始日期。 I know that it's always a Sunday, but how do I know whether it's the first Sunday in calendar February, or the last Sunday in calendar January? 我知道它总是一个星期日,但是我怎么知道它是2月日历中的第一个星期日,还是1月日历中的最后一个星期日?

(Fortunately for this function's purposes I can ignore the occasional 53rd week; I can just return week 52 in that case. That's because (I'm told) that years having 53 fiscal weeks are not predictable (at this company anyway) and are determined by human whim.) (幸运的是,出于此功能的目的,我可以忽略偶发的第53周;在这种情况下,我只能返回第52周。这是因为(我被告知)拥有53个会计周的年份是无法预测的(无论如何,这家公司)由人类的异想天开。)

Any suggestions? 有什么建议么?

UPDATE: 更新:

I've been given a document with FY calendars for our company from FY2005 through FY2017. 从2005财年到2017财年,我收到了一份包含公司日历的文档。 The pattern I'm seeing is that: 我看到的模式是:

  • If Feb 1st is a Monday, Tuesday, or Wednesday, then first day of FY is last Sunday of January. 如果2月1日是星期一,星期二或星期三,则FY的第一天是一月的最后一个星期日。
  • If Feb 1st is a Thursday, Friday, Saturday, or Sunday, then first day of FY is first Sunday of February. 如果2月1日是星期四,星期五,星期六或星期日,则FY的第一天是2月的第一个星期日。

I think that gives me what I need. 我认为这满足了我的需求。

I think the first step is to find the day of the week for Feb 1st of the input date's year. 我认为第一步是找到输入日期年份的2月1日的星期几。

Next, find the first day of the FY, which is based on that Feb 1st day of week. 接下来,找到FY的第一天,该日期基于一周的2月1日。 If it's a Mon, Tue, or Wed, then first day of FY is last Sunday of January. 如果是星期一,星期二或星期三,则FY的第一天是一月的最后一个星期日。 Otherwise, first day of FY is first Sunday of January. 否则,FY的第一天是一月的第一个星期日。

Next, determine whether the input date is in that FY or the one before. 接下来,确定输入日期是在该财政年度还是之前。

Then, if the input date is in the previous FY, get the first day of that FY. 然后,如果输入日期在上一个财政年度,则获取该财政年度的第一天。

Next, count the days from that first day of the FY, to the day of the input date. 接下来,计算从FY的第一天到输入日期的天数。 Divide by 7, rounding UP to the next integer. 除以7,将UP舍入到下一个整数。 That is the FY week. 那是风云周。

At that point, I'll know the input date's FY year and FY week, and can return it. 到那时,我将知道输入日期的FY年和FY周,并可以将其返回。

UPDATE: 更新:

Here's what I have; 这就是我所拥有的; it works in my tests: 它在我的测试中有效:

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

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

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