如何創建一個類似於DateDiff的查詢/ vba函數,將結果拆分爲每月的天數(即2010年1月1日 - 2010年2月3日= 1月31日,2月: 3(忽略格式))。DateDiff分成幾個月access/vba
回答
好吧,我想我明白你想要做什麼。
首先你需要返回一個月的天數的函數,給出年份和月份(你要知道當年佔到由於閏年二月變化的天數):
Function DaysInMonth(month As Integer, year As Integer) As Integer
If month < 1 Or month > 12 Then
DaysInMonth = -1
Else
DaysInMonth = Day(DateSerial(year, month + 1, 1) - 1)
End If
End Function
我已經寫的函數GetMonthDays,是以開始日期和結束日期,並返回整數數組(1到12),其包含在各月的天數,指定的開始和結束日期之間。開始日期和結束日期可以是相隔的任意年數,如果有必要,它將積累每個月的總天數。
例如,函數調用,例如:
Dim months() As Integer
months = GetMonthDays(#6/13/2011#, #8/1/2011#)
將返回的數組[0,0,0,0,0,18,31,1,0,0,0,0]
的呼叫,例如:
months = GetMonthDays(#12/25/2010#, #1/15/2011#)
返回[15,0,0,0,0,0,0,0,0,0,0,7]
在多個年,對於例如:
months = GetMonthDays(#12/25/2009#, #1/15/2011#)
它會返回[46,28,31,30,31,30,31,31,30,31,30,38]
你可以看到,它已經跨越積累的天數兩個Januarys(31 + 15)和兩個12月(31 + 7)。我不是100%確定這是你想要的,但是如果給定的日期範圍跨越12個月以上,對我來說是有意義的。
基本上,函數循環遍歷開始和結束日期之間的每個月,並累積每個日期。第一個月和最後一個月是需要一些計算的特殊情況,否則它只是該月的天數。
的功能如下,減去錯誤檢查:
Function GetMonthDays(startDate As Date, endDate As Date) As Integer()
Dim months(1 To 12) As Integer
Dim monthStart As Integer
Dim monthEnd As Integer
Dim yearStart As Integer
Dim yearEnd As Integer
Dim monthLoop As Integer
Dim yearLoop As Integer
' initialise months array to all zeros
For monthLoop = 1 To 12
months(monthLoop) = 0
Next monthLoop
monthStart = month(startDate)
monthEnd = month(endDate)
yearStart = year(startDate)
yearEnd = year(endDate)
monthLoop = monthStart
yearLoop = yearStart
Do Until yearLoop >= yearEnd And monthLoop > monthEnd
If yearLoop = yearStart And monthLoop = monthStart Then
months(monthLoop) = months(monthLoop) + (DaysInMonth(monthLoop, yearLoop) - Day(startDate) + 1)
ElseIf yearLoop = yearEnd And monthLoop = monthEnd Then
months(monthLoop) = months(monthLoop) + Day(endDate)
Else
months(monthLoop) = months(monthLoop) + DaysInMonth(monthLoop, yearLoop)
End If
If monthLoop < 12 Or (monthLoop = 12 And yearLoop = yearEnd) Then
monthLoop = monthLoop + 1
Else
monthLoop = 1
yearLoop = yearLoop + 1
End If
Loop
GetMonthDays = months
End Function
我一直在使用功能測試它,例如:
Sub TestRun()
Dim months() As Integer
months = GetMonthDays(#12/25/2009#, #1/15/2011#)
MsgBox _
months(1) & vbCrLf & _
months(2) & vbCrLf & _
months(3) & vbCrLf & _
months(4) & vbCrLf & _
months(5) & vbCrLf & _
months(6) & vbCrLf & _
months(7) & vbCrLf & _
months(8) & vbCrLf & _
months(9) & vbCrLf & _
months(10) & vbCrLf & _
months(11) & vbCrLf & _
months(12)
End Sub
這應該是一個很好的起點爲你至少。祝你好運!
編輯:在12月結束日期時進行小的更改以糾正錯誤。 –
對於需要類似功能的其他人,當日期在同一個月(#11/5/2011#,#11/20/2011#返回26)時,上述功能可能會失敗。 –
- 1. Spark - Datediff幾個月?
- 2. 將SQL數據分成幾個月
- 3. DATEDIFF(月)
- 4. SQL將月份價值分成幾周
- 5. 將YYYYMMDD格式的日期分成幾個月?
- 6. 在幾個月分發每年數成倍
- 7. 使用Javascript:將事件拆分成幾個月
- 8. 分岔記錄各自到幾個月
- 9. aspclassic - Datediff每個月處理30天
- 10. DATEDIFF結果月平均值
- 11. 則DateDiff爲一把umbraco月
- 12. Python的SQLAlchemy的:由DATEDIFF月
- 13. DateDiff計算問題 - 月
- 14. 計算幾個月
- 15. 在VB中使用DateDiff()來查找兩個日期在幾個月和幾天之間的差異
- 16. 將一列分成幾個
- 17. 使用月份表對幾個月進行分組
- 18. 爲什麼jquery datepicker將幾個月改爲幾個月?
- 19. 用幾個月和數字填充組合框幾個月
- 20. 拆分數據分成幾個部分
- 21. 將某個月分爲幾天和幾天
- 22. 查看幾個月的活躍月份
- 23. JavaScript循環槽幾個月和幾年
- 24. 熊貓:轉換幾天到幾個月
- 25. NgbDatepicker只顯示幾個月和幾年
- 26. 拆分models.py分成幾個文件
- 27. 分裂的JavaScript分成幾個文件
- 28. 將一個字節分成幾部分
- 29. 上個月的前幾天
- 30. 比較幾個月和jQuery
因此,每個結果將有兩個日期列表示日期範圍,並且您希望每個結果顯示每個月的天數? – Banjoe
輸入將包含兩個日期列。結果將是12個月,每個月的天數。 (一個DateDiff在所有12個月中分割)。 –
如果不是,則計算本月最後12個月的月份(所以在本月的10月份中,比較從10年前到今天的日期將導致從去年11月初至今的計算) 。 –