返回年末日期的功能

发布于 2025-02-07 04:39:40 字数 529 浏览 0 评论 0原文

我有许多财务报告,并非总是在12月31日结束。

我正在努力的功能应为年终日期,并返回现在日期和下一年末日期之间的天数,除以365。前瞻性,因此,如果年终日期是昨天,则该功能应返回364天,而不是1。)

以下尝试回报1。我相信这与约会夫有关。

Function dateCalc(ye As Date)
    Dim today As Date
    Dim x As Integer
    
    today = Date
    
    If ye < today Then
        x = (365 - DateDiff("d", ye, today)) / 365

    ElseIf ye > today Then
        x = DateDiff("d", ye, today) / 365

    Else
        x = 0
    
    End If
    
    dateCalc = x
End Function

I have many financial reports, not always ending on Dec 31.

The function I'm working on should take the year-end date and return the number of days between the present date and the next year-end date, divided by 365. (Forward-looking, so if the year-end date was yesterday then the function should return 364 days, rather than 1.)

The attempt below returns 1. I believe it has to do with DateDiff.

Function dateCalc(ye As Date)
    Dim today As Date
    Dim x As Integer
    
    today = Date
    
    If ye < today Then
        x = (365 - DateDiff("d", ye, today)) / 365

    ElseIf ye > today Then
        x = DateDiff("d", ye, today) / 365

    Else
        x = 0
    
    End If
    
    dateCalc = x
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2025-02-14 04:39:40

使用代码的以下衍生物,

Function dateCalc(ByVal YE As Date) As Double
  If YE < Today Then
    dateCalc = (365 - DateDiff("d", YE, Date)) / 365
  ElseIf YE > Today Then
    dateCalc = DateDiff("d", YE, Date) / 365
  Else
    dateCalc = 0
  End If
End Function

在6/14/2022日期,我们看到以下内容:

  • ?DateCalc(#6/15/2022#)生产-0.00273972602739726
  • ?datecalc(#6/13/2022#)产生0.997260273972603

请注意,在ye&gt;今天案例,约会夫为-1。如果您想要积极的话,请否定它。从问题中尚不清楚这是否是所需的输出,但是主要问题似乎是使用Integer而不是double> double

Using the below derivative of your code,

Function dateCalc(ByVal YE As Date) As Double
  If YE < Today Then
    dateCalc = (365 - DateDiff("d", YE, Date)) / 365
  ElseIf YE > Today Then
    dateCalc = DateDiff("d", YE, Date) / 365
  Else
    dateCalc = 0
  End If
End Function

On the date 6/14/2022, we see the following:

  • ?dateCalc(#6/15/2022#) produces -0.00273972602739726
  • ?dateCalc(#6/13/2022#) produces 0.997260273972603

Note that in the YE > Today case, the DateDiff is -1. If you wanted a positive, negate it. It wasn't clear from the question if this was the desired output, but the primary problem appears to be the use of Integer as opposed to Double.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文