我知道Sql Server 有一些方便的内置季度内容,但是 .Net 原生DateTime对象呢?加、减和遍历四分之一的最佳方法是什么?
使用 VB 特有的DateAdd()函数是一件坏事吗?例如:
Dim nextQuarter As DateTime = DateAdd(DateInterval.Quarter, 1, DateTime.Now)
编辑:扩展@bslorence 的功能:
Public Shared Function AddQuarters(ByVal originalDate As DateTime, ByVal quarters As Integer) As Datetime
Return originalDate.AddMonths(quarters * 3)
End Function
扩展@Matt 的功能:
Public Shared Function GetQuarter(ByVal fromDate As DateTime) As Integer
Return ((fromDate.Month - 1) \ 3) + 1
End Function
编辑:这里还有几个方便的功能:
Public Shared Function GetFirstDayOfQuarter(ByVal originalDate As DateTime) As DateTime
Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate) - 1)
End Function
Public Shared Function GetLastDayOfQuarter(ByVal originalDate As DateTime) As DateTime
Return AddQuarters(New DateTime(originalDate.Year, 1, 1), GetQuarter(originalDate)).AddDays(-1)
End Function