[英]Is there a .NET way to store time periods like my custom class?
是否有一個本機.NET類來處理這樣的時間跨度? 我找不到一個。
有沒有接近?
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
和派生的月期:
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
簡短的回答:我認為沒有任何內置類接近於此。 可能最接近的是TimeSpan,但這只是一個相對跨度,沒有絕對開始或結束日期的概念。
使用.NET標准TimeSpan。 如有必要,創建一個繼承TimeSpan的新類,但根據需要添加新方法。
我沒有閱讀你的所有代碼。 您的實現可能提供更多功能,但.Net Framework實現稱為TimeSpan。
它位於System命名空間中。
http://msdn.microsoft.com/en-us/library/system.timespan(v=VS.90).aspx
TimeSpan僅限天數作為最大單位。 即沒有數月或數年的衡量標准。 如果需要,它可以擴展。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.