如何创建一个类似于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月初至今的计算) 。 –