有没有一种.NET的方法可以存储时间段,类似于我的自定义类?

5

是否有一个.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
1
将DateTime转换为Ticks。然后,您可以使用long运算符内置函数。 - WeNeedAnswers
1
我不确定这与 .net 的 TimeSpan 有任何关系。OP 实际上并不关心范围的长度,而是范围本身的端点。 - Greg
3个回答

4

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


谢谢。我看到了TimeSpan对象,但它似乎不完整,因为它没有开始和结束日期。由于TimeSpan是一个结构体,我无法从中继承,但也许我可以在我的类上创建一个TimeSpan属性。 - VJK

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

TimeSpan是一个结构体,而不是一个类。你不能从它继承。我添加了这个属性: 公共只读属性TimeSpan As TimeSpan 获取 返回Me.ToDate - Me.FromDate 结束获取 结束属性 - VJK

1

我没有阅读你的全部代码。虽然你的实现可能提供更多的功能,但.Net Framework 的实现被称为 TimeSpan。

它位于 System 命名空间中。

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

TimeSpan的最大单位是天。也就是说,没有以月或年为单位的测量。不过,如果需要的话可以进行扩展。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接