VB.Net 2.0을 사용하여 ASP.Net에서 분기(분기별 날짜) 작업을 어떻게 합니까?
문제
나는 그것을 안다 SQL Server에는 편리한 분기별 기능이 내장되어 있습니다. 하지만 .Net 네이티브는 어떻습니까? 날짜 시간 물체?분기를 더하고, 빼고, 순회하는 가장 좋은 방법은 무엇입니까?
그건 나쁜 것™ VB 전용을 사용하려면 날짜추가() 기능?예:
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
해결책
다음과 같은 방법으로 날짜의 분기를 계산할 수 있다는 것을 알고 있습니다.
Dim quarter As Integer = (someDate.Month - 1) \ 3 + 1
Visual Studio 2008을 사용하는 경우 다음을 살펴보고 DateTime 클래스에 추가 기능을 추가해 볼 수 있습니다. 확장 방법.
다른 팁
이건 어때:
Dim nextQuarter As DateTime = DateTime.Now.AddMonths(3);
한 가지 기억해야 할 점은 모든 회사가 매월 말일에 분기를 끝내는 것은 아니라는 것입니다.
Public Function GetLastQuarterStart() As Date
GetLastQuarterStart = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")
End Function
Public Function GetLastQuarterEnd() As Date
Dim LastQuarterStart As Date = DateAdd(DateInterval.Quarter, -1, DateTime.Now).ToString("MM/01/yyyy")
Dim MM As String = LastQuarterStart.Month
Dim DD As Integer = 0
Dim YYYY As String = LastQuarterStart.Year
Select Case MM
Case "01", "03", "05", "07", "08", "10", "12"
DD = 31
Case "02"
Select Case YYYY
Case "2012", "2016", "2020", "2024", "2028", "2032"
DD = 29
Case Else
DD = 28
End Select
Case Else
DD = 30
End Select
Dim LastQuarterEnd As Date = DateAdd(DateInterval.Month, 2, LastQuarterStart)
MM = LastQuarterEnd.Month
YYYY = LastQuarterEnd.Year
Return String.Format("{0}/{1}/{2}", MM, DD, YYYY)
End Function
Matt Blaine의 답변을 확장하면 다음과 같습니다.
Dim intQuarter As Integer = Math.Ceiling(MyDate.Month / 3)
이것이 속도 이점을 추가할지 여부는 확실하지 않지만 더 깨끗해 보입니다. IMO
제휴하지 않습니다 StackOverflow