2010-11-12 18 views
5

是否有一個處理這種時間跨度的本機.NET類?我一直無法找到一個。是否有.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 
+3

你看過TimeSpan嗎? – Nate 2010-11-12 21:40:06

+1

將DateTime轉換爲Ticks。然後您可以使用長操作員插件。 – WeNeedAnswers 2010-11-12 21:43:41

+1

我不確定這與.net TimeSpan有什麼關係。 OP對範圍的長度並不感興趣,但範圍本身的終點。 – Greg 2010-11-12 21:48:49

回答

4

簡短回答:我不認爲有任何內置類與此接近。可能最接近的是TimeSpan,但這只是一個沒有絕對開始或結束日期概念的相對跨度。

+0

謝謝。我看到了TimeSpan對象,它看起來不完整,因爲它沒有開始和結束日期。由於TimeSpan是一個結構,我不能繼承它,但也許我可以在我的類上創建TimeSpan屬性 – VJK 2010-11-12 23:19:30

2

使用.NET標準TimeSpan。如有必要,創建一個繼承TimeSpan的新類,但根據需要添加新方法。

+0

TimeSpan是一個結構,而不是一個類。你不能從它繼承。 Ia dded tghjis屬性:公開只讀屬性TimeSpan As TimeSpan Get Return Me.ToDate - Me.FromDate End Get End Property – VJK 2010-11-13 03:46:05

相關問題