是否有.NET方式来存储像我的自定义类这样的时间段?

时间:2010-11-12 21:35:35

标签: .net date

是否有一个本机.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 个答案:

答案 0 :(得分:4)

简短回答:我认为没有任何内置类接近于此。可能最接近的是TimeSpan,但这只是一个相对跨度,没有绝对开始或结束日期的概念。

答案 1 :(得分:2)

使用.NET标准TimeSpan。如有必要,创建一个继承TimeSpan的新类,但根据需要添加新方法。

答案 2 :(得分:1)

我没有阅读你的所有代码。您的实现可能提供更多功能,但.Net Framework实现称为TimeSpan。

它位于系统命名空间中。

http://msdn.microsoft.com/en-us/library/system.timespan(v=VS.90).aspx

TimeSpan仅限天数作为最大单位。即没有数月或数年的衡量标准。如果需要,它可以扩展。