如何創建一個類似於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. DATEDIFF(月)
- 2. DATEDIFF結果月平均值
- 3. 則DateDiff爲一把umbraco月
- 4. 計算幾個月
- 5. 使用月份表對幾個月進行分組
- 6. 用幾個月和數字填充組合框幾個月
- 7. 查看幾個月的活躍月份
- 8. 熊貓:轉換幾天到幾個月
- 9. JavaScript循環槽幾個月和幾年
- 10. NgbDatepicker只顯示幾個月和幾年
- 11. 拆分models.py分成幾個文件
- 12. 幾個月前在ruby
- 13. 比較幾個月和jQuery
- 14. 獲取前幾個月
- 15. 清單12前幾個月
- 16. 上個月的前幾天
- 17. 分組一個QRadioButton成幾個QButtonGroups
- 18. Spark將一個DStream分成幾個RDD
- 19. 將`UITableViewCell`分成幾部分
- 20. 使用DateDiff來查找持續時間在幾分鐘內
- 21. 如何將單個日期分組爲幾個月
- 22. DATEDIFF(DD ...返回月份數據
- 23. MySQL - datediff天(保留幾位十進制)
- 24. SQLServer的分數DATEDIFF?
- 25. 如何長數字串分成個月
- 26. 通過幾個月和幾年的幾天的PHP循環
- 27. Android-DOB從幾年,幾個月和幾天
- 28. 劈裂一列分成幾個欄
- 29. 如何按數據透視表將幾天和幾個月內的列分組?
- 30. 生成過去幾個月並加入無關數據
因此,每個結果將有兩個日期列表示日期範圍,並且您希望每個結果顯示每個月的天數? – Banjoe
輸入將包含兩個日期列。結果將是12個月,每個月的天數。 (一個DateDiff在所有12個月中分割)。 –
如果不是,則計算本月最後12個月的月份(所以在本月的10月份中,比較從10年前到今天的日期將導致從去年11月初至今的計算) 。 –