获取上一个分期付款日期

时间:2015-03-24 14:00:23

标签: vba ms-access access-vba

我创建了一个功能,我将传递日期期限,结果将是日期,这是一个已过期日期和期限的前一期。以下是示例。

Date (In)     |    Term (In)    |    Date (Out)
--------------+-----------------+--------------
22/02/2015    |    Monthly      |    22/03/2015
22/02/2015    |    Quarterly    |    22/02/2015
01/01/2015    |    Monthly      |    01/03/2015
24/03/2015    |    Annually     |    24/03/2015

术语价值可以是每月,每季度,每年。这是我目前的功能,但没有给我所需的输出。

Public Function getPrevInst(tmpDate As Date, tmpPeriod As String) As Date
    Dim monthDiff As Integer, modVal As Integer, tmpFuncDate As Date, tmpMonDiff As Long

    Select Case tmpPeriod
        Case "Monthly"
            modVal = 1
        Case "Quarterly"
            modVal = 3
        Case Else
            modVal = 12
    End Select

    monthDiff = DateDiff("m", tmpDate, LstDayPrevMnth(Date))

    tmpMonDiff = IIf(monthDiff > 0, monthDiff - (monthDiff Mod modVal), IIf(monthDiff < 0, 0, 1))

    tmpFuncDate = DateAdd("m", tmpMonDiff, tmpDate)

    If tmpFuncDate >= Date Then
        getPrevInst = DateAdd("m", monthDiff, tmpDate)
    Else
        getPrevInst = tmpFuncDate
    End If
End Function

Public Function LstDayPrevMnth(InDate As Date) As Date
    LstDayPrevMnth = DateSerial(Year(InDate), Month(InDate), 0)
End Function

我的结果(显然是错误的),

Date (In)     |    Term (In)    |    Date (Out)
--------------+-----------------+--------------
22/02/2015    |    Monthly      |    22/03/2015
22/02/2015    |    Quarterly    |    22/03/2015
01/01/2015    |    Monthly      |    01/02/2015
24/03/2015    |    Annually     |    24/02/2015

如果您有更好的方法找到上一期也会受到欢迎。如果我的代码非常错误。

Layman的任期

彼得向我借了一笔贷款。他计划偿还Monthly,他的第一部分是22 February 2015。如果我打开他的记录,我需要查看最后一笔付款到期/付款的时间。所以今天(2015年3月24日),我会得到他的第一笔付款22 February 201522 March 2015(截至3月22日是两天前)。因此,上一期付款日期为 2015年3月22日

汤姆借了我的贷款。他计划每月偿还,他的第一部分是01 January 2015。如果我打开他的记录,我需要查看最后一笔付款到期/付款的时间。所以今天(2015年3月24日),我会得到他的第一笔付款01 January 201501 February 201501 March 2015(截至3月1日是23天前)。因此,上一期付款日期 2015年3月1日

哈利向我借了一笔贷款。他打算季度报销,他的第一部分是22 February 2015。如果我打开他的记录,我需要查看最后一笔付款到期/付款的时间。所以今天(2015年3月24日),我会得到他的第一笔付款22 March 2015(就是这样,下一笔付款尚未到期,直到22 June 2015尚未发生)。因此,上一期付款日期为 2015年3月22日

注意: Cross发布于:http://www.access-programmers.co.uk/forums/showthread.php?t=276041

1 个答案:

答案 0 :(得分:0)

这比你想象的更敏感,因为一个月不是&#34;一个月&#34;。 但是 - 使用下面的月份功能 - 您可以精确计算日期:

DateLast = DateAdd("m", Months(DateFirst, Date), DateFirst)
DateLast = DateAdd("m", (Months(DateFirst, Date) \ 3) * 3, DateFirst)
DateLast = DateAdd("m", (Months(DateFirst, Date) \ 12) * 12, DateFirst)

注意使用\。

的整数除法
Public Function Months( _
  ByVal datDate1 As Date, _
  ByVal datDate2 As Date, _
  Optional ByVal booLinear As Boolean) _
  As Integer

' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
'   negative differences
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
'   3, 2, 1, 0,  0, -1, -2
' If booLinear is True, the sequence will be:
'   3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().

' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.

  Dim intDiff   As Integer
  Dim intSign   As Integer
  Dim intMonths As Integer

  ' Find difference in calendar months.
  intMonths = DateDiff("m", datDate1, datDate2)
  ' For positive resp. negative intervals, check if the second date
  ' falls before, on, or after the crossing date for a 1 month period
  ' while at the same time correcting for February 29. of leap years.
  If DateDiff("d", datDate1, datDate2) > 0 Then
    intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
    intDiff = Abs(intSign < 0)
  Else
    intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
    If intSign <> 0 Then
      ' Offset negative count of months to continuous sequence if requested.
      intDiff = Abs(booLinear)
    End If
    intDiff = intDiff - Abs(intSign < 0)
  End If

  ' Return count of months as count of full 1 month periods.
  Months = intMonths - intDiff

End Function