【发布时间】:2020-05-27 11:08:15
【问题描述】:
我在一家财政年度非常奇怪的公司工作。他们的财政年度从 11 月开始。我想使用 DatePart 函数来获取指定日期之外的月、季度和年数据,但我需要确保根据一年中的第一周对它们进行排序。如何更改该值以使其从 11 月开始?
【问题讨论】:
标签: vba ms-access sql-date-functions
我在一家财政年度非常奇怪的公司工作。他们的财政年度从 11 月开始。我想使用 DatePart 函数来获取指定日期之外的月、季度和年数据,但我需要确保根据一年中的第一周对它们进行排序。如何更改该值以使其从 11 月开始?
【问题讨论】:
标签: vba ms-access sql-date-functions
诀窍是根据财政年度日期创建一个伪日历日期。然后你可以执行所有常用的计算等。
这里有一些代码 sn-ps 可以帮助您入门:
Option Explicit
' A fiscal year is designated as the calendar year in which it ends.
' For example, if the fiscal year runs from June 1, 2022, to May 31, 2023,
' it could be designated:
'
' financial year 2023
' fiscal year 2023
' FY2023
'
' Initially, call function SetFinancialStartMonth or SetFinancialEndMonth to
' define the financial/fiscal year.
' Constants.
' Default start day and month of the financial/fiscal year applied a
' neutral year for storing the values as a Date value.
Private Const DefaultStart As Date = #1/1/2000#
' Maximum day value valid for any month and year.
Private Const MaxDayAllMonthsValue As Integer = 28
' Statics.
' Local static variable to hold the selected start day and month of the financial/fiscal year.
' These are defined and read by the function:
'
' SetFinancialStart
' SetFinancialEnd
'
Private FinancialStart As Date
' Returns a calendar date as its equivalent pseudo date of the financial/fiscal year.
' This is mostly useful for creating search criteria, or to obtain the quarter, the
' month, or the year of the financial/fiscal year for a calendar date.
'
' 2019-10-06. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function DateFinancial( _
ByVal CalendarDate As Date) _
As Date
Dim StartMonth As Integer
Dim StartDay As Integer
Dim Days As Integer
Dim FinancialDate As Date
StartMonth = Month(GetFinancialStart)
StartDay = Day(GetFinancialStart)
FinancialDate = DateAdd("m", MinMonthValue - StartMonth + MaxMonthValue, CalendarDate)
' Correct ultimo month dates of CalendarDate.
If Day(CalendarDate) > Day(FinancialDate) Then
Days = DaysInMonth(CalendarDate)
Else
Days = DaysInMonth(FinancialDate)
End If
FinancialDate = DateAdd("d", MinDayValue - StartDay + Days, FinancialDate)
DateFinancial = FinancialDate
End Function
' Returns the financial start month and day as a Date value applied a neutral year.
' If the start month and day have not been set, the default value is returned.
'
' 2019-01-26. Gustav Brock. Cactus Data ApS, CPH.
'
Public Function GetFinancialStart() As Date
If FinancialStart = #12:00:00 AM# Then
' Set default value for start date.
SetFinancialStart
End If
GetFinancialStart = FinancialStart
End Function
' Sets the start day and month of the financial/fiscal year as a
' date value applied a neutral year.
'
' The start month can be any month.
' The start day can be any day less than or equal 28, which is the
' highest day value valid for any month.
'
' Default value is January 1st.
'
' 2019-01-26. Gustav Brock. Cactus Data ApS, CPH.
'
Public Sub SetFinancialStart( _
Optional ByVal StartMonth As Integer = MinMonthValue, _
Optional ByVal StartDay As Integer = MinDayValue)
Const MaxMonthValue As Integer = 12
Const MinMonthValue As Integer = 1
Const MinDayValue As Integer = 1
' Validate input.
If StartMonth < MinMonthValue Or _
StartMonth > MaxMonthValue Or _
StartDay < MinDayValue Or _
StartDay >= MaxDayAllMonthsValue Then
Err.Raise DtError.dtInvalidProcedureCallOrArgument
End If
FinancialStart = DateSerial(Year(DefaultStart), StartMonth, StartDay)
End Sub
' Sets - based on the end day and month - the start day and month of the
' financial/fiscal year as a date value applied a neutral year.
'
' The end month can be any month.
' The end day can be any day larger than 1. If end day is larger than 28,
' which is the highest day value valid for any month, start day is set to 1.
'
' Default value is January 1st.
'
' 2019-01-26. Gustav Brock. Cactus Data ApS, CPH.
'
Public Sub SetFinancialEnd( _
Optional ByVal EndMonth As Integer = MaxMonthValue, _
Optional ByVal EndDay As Integer = MaxDayValue)
Dim StartMonth As Integer
Dim StartDay As Integer
' Validate input.
If EndMonth < MinMonthValue Or _
EndMonth > MaxMonthValue Or _
EndDay < MinDayValue Or _
EndDay > MaxDayValue Then
Err.Raise DtError.dtInvalidProcedureCallOrArgument
End If
If EndDay < MaxDayAllMonthsValue - 1 Then
' Set start date of the financial year as the day after the end day.
StartMonth = EndMonth
StartDay = EndDay + 1
Else
' Set start date of the financial year as primo next month.
StartMonth = EndMonth Mod MonthsPerYear + 1
StartDay = 1
End If
SetFinancialStart StartMonth, StartDay
End Sub
' Returns the count of days of the month of Date1.
'
' 2016-02-14. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DaysInMonth( _
ByVal Date1 As Date) _
As Integer
Const MaxDateValue As Date = #12/31/9999#
Const MaxDayValue As Integer = 31
Dim Days As Integer
If DateDiff("m", Date1, MaxDateValue) = 0 Then
Days = MaxDayValue
Else
Days = Day(DateSerial(Year(Date1), Month(Date1) + 1, 0))
End If
DaysInMonth = Days
End Function
【讨论】: