Có một lớp .NET gốc xử lý timespan như thế này không? Tôi đã không thể tìm thấy một.Có cách nào .NET để lưu trữ các khoảng thời gian như lớp tùy chỉnh của tôi không?
Có ai đến gần không?
Public Class Period
Property FromDate As Date
Property ToDate As Date
Public Sub New(ByVal fromDate As Date, ByVal toDate As Date)
If fromDate > toDate Then
Throw New ArgumentException("fromDate must be less than Or equal toDate")
End If
_FromDate = fromDate
_ToDate = toDate
End Sub
Public Overloads Shared Operator =(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return period1.FromDate = period2.FromDate AndAlso
period1.ToDate = period2.ToDate
End Operator
Public Overloads Shared Operator <>(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return Not period1 = period2
End Operator
Public Overloads Shared Operator <(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return period1.FromDate < period2.FromDate
End Operator
Public Overloads Shared Operator >(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return period1.FromDate > period2.FromDate
End Operator
Public Overloads Shared Operator >=(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return period1.FromDate >= period2.FromDate
End Operator
Public Overloads Shared Operator <=(ByVal period1 As Period,
ByVal period2 As Period) As Boolean
Return period1.FromDate <= period2.FromDate
End Operator
Public Function Contains(ByVal checkDate As Date) As Boolean
Return checkDate >= Me.FromDate AndAlso
checkDate < Me.ToDate
End Function
Public Overrides Function ToString() As String
Return Format(_FromDate, "MMM-yyyy") & "-" & Format(_ToDate, "MMM-yyyy")
End Function
End Class
và một giai đoạn Tháng nguồn gốc:
Public Class MonthPeriod : Inherits Period
Private _MonthStartDate As Date
Public Sub New(ByVal dateInMonth As Date)
'Everything >= the 1st of the month to < the 1st of the next month
MyBase.New(New Date(dateInMonth.Year, dateInMonth.Month, 1),
New Date(dateInMonth.Year, dateInMonth.Month, 1).AddMonths(1))
_MonthStartDate = New Date(dateInMonth.Year, dateInMonth.Month, 1)
End Sub
Public Overrides Function ToString() As String
Return Format(_MonthStartDate, "MMM-yyyy")
End Function
End Class
Bạn đã xem TimeSpan chưa? – Nate
Chuyển đổi DateTime thành Ticks. Sau đó, bạn có thể sử dụng các buit-ins của nhà khai thác dài. – WeNeedAnswers
Tôi không chắc điều này có liên quan gì đến TimeSpan .net không. Các OP không thực sự quan tâm đến khoảng thời gian bao lâu, mà là các điểm cuối của chính phạm vi đó. – Greg