如何计算一年中的某一天是在哪一天?

我已经find了很多function来确定某一天的某一天是哪一天

Function Yearday(dat As Date) As Integer 'Purpose: does the same as day for month 'Use to access an array where the index corresponds to days Yearday = DateDiff("d", CDate("1/1/" & Year(dat)), dat) + 1 End Function 

但我还没有find任何function来确定date,基于特定年份的一天。

函数签名看起来像这样:

 Function getDateBasedOnYearAndDayOfYear(year As Integer, dayOfYear As Integer) AS Date 

也许这样的事情,

 Function getDateBasedOnYearAndDayOfYear(year As Integer, dayOfYear As Integer) AS Date getDateBasedOnYearAndDayOfYear = dateserial(year, 1, 0) + dayofyear end function 

只要创build一个新的date与去年1月的第一个,然后添加天数。 这在VBA中非常简单,因为date只是整数部分的两倍,代表了自1899年12月31日以来的天数。

从这开始:

 Function getDateBasedOnYearAndDayOfYear(year As Integer, dayOfYear As Integer) As Date Dim result As Date result = DateSerial(year, 1, 1) getDateBasedOnYearAndDayOfYear = result + dayOfYear - 1 End Function 

我可能会添加边界检查等,即一年中的第-634天没有太大的意义。

编辑:

只是testingDateSerial – 你甚至不需要做math:

 Function getDateBasedOnYearAndDayOfYear(year As Integer, dayOfYear As Integer) As Date getDateBasedOnYearAndDayOfYear = DateSerial(year, 1, dayOfYear) End Function