gpt4 book ai didi

.net - 是否有.NET方式来存储类似于自定义类的时间段?

转载 作者:行者123 更新时间:2023-12-04 18:15:40 25 4
gpt4 key购买 nike

是否有一个本地的.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 - 是否有.NET方式来存储类似于自定义类的时间段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4169242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com