2010-11-12 41 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