如何从日期时间选择器值将年龄显示为年,月,日。例如:
Datetimepicker value = #1/11/2014#
Today = #1/12/2015#.
最终结果将为“ 1年0个月1天”。但是获得该结果不仅仅只是减去DateTime.Year
值,等等。有人知道关于它的数学公式或数学计算吗?
这是一个DateTimeSpan
类型,它返回一个对象,如,TimeSpan
表示两个日期之间经过的年,月,日。它遍历时间单位以递增时间单位,直到较小的日期与较大的日期匹配。然后将所得的增量返回到新DateTimeSpan
对象中。
Public Structure DateTimeSpan
Private _Years As Integer
Public ReadOnly Property Years As Integer
Get
Return _Years
End Get
End Property
Private _Months As Integer
Public ReadOnly Property Months As Integer
Get
Return _Months
End Get
End Property
Private _Days As Integer
Public ReadOnly Property Days As Integer
Get
Return _Days
End Get
End Property
Private _Hours As Integer
Public ReadOnly Property Hours As Integer
Get
Return _Hours
End Get
End Property
Private _Minutes As Integer
Public ReadOnly Property Minutes As Integer
Get
Return _Minutes
End Get
End Property
Private _Seconds As Integer
Public ReadOnly Property Seconds As Integer
Get
Return _Seconds
End Get
End Property
Private _MilliSeconds As Integer
Public ReadOnly Property MilliSeconds As Integer
Get
Return _MilliSeconds
End Get
End Property
' the ctor for the result
Private Sub New(y As Integer, mm As Integer, d As Integer,
h As Integer, m As Integer, s As Integer,
ms As Integer)
_Years = y
_Months = mm
_Days = d
_Hours = h
_Minutes = m
_Seconds = Seconds
_MilliSeconds = ms
End Sub
' private time unit tracker when counting
Private Enum Unit
Year
Month
Day
Complete
End Enum
Public Shared Function DateSpan(dt1 As DateTime,
dt2 As DateTime) As DateTimeSpan
' we dont do negatives
If dt2 < dt1 Then
Dim tmp = dt1
dt1 = dt2
dt2 = tmp
End If
Dim thisDT As DateTime = dt1
Dim years As Integer = 0
Dim months As Integer = 0
Dim days As Integer = 0
Dim level As Unit = Unit.Year
Dim span As New DateTimeSpan()
While level <> Unit.Complete
Select Case level
' add a year until it is larger;
' then change the "level" to month
Case Unit.Year
If thisDT.AddYears(years + 1) > dt2 Then
level = Unit.Month
thisDT = thisDT.AddYears(years)
Else
years += 1
End If
Case Unit.Month
If thisDT.AddMonths(months + 1) > dt2 Then
level = Unit.Day
thisDT = thisDT.AddMonths(months)
Else
months += 1
End If
Case Unit.Day
If thisDT.AddDays(days + 1) > dt2 Then
thisDT = thisDT.AddDays(days)
Dim thisTS = dt2 - thisDT
' create a new DTS from the values caluclated
span = New DateTimeSpan(years, months, days, thisTS.Hours,
thisTS.Minutes, thisTS.Seconds,
thisTS.Milliseconds)
level = Unit.Complete
Else
days += 1
End If
End Select
End While
Return span
End Function
End Structure
Dim dts As DateTimeSpan = DateTimeSpan.DateSpan(#2/11/2010#, #10/21/2013#)
Console.WriteLine("{0} Yrs, {1} Months and {2} Days",
dts.Years.ToString, dts.Months.ToString, dts.Days.ToString)
这将产生相同的结果:
Dim dtStart As DateTime = #2/11/2010#
Dim dtEnd As new DateTime(2013, 10, 21)
' this is NOT a date and wont work:
Dim myDt = "2/11/2010" ' its a string!
Dim dts As DateTimeSpan = DateTimeSpan.DateSpan(dtEnd, dtStart)
结果: 3 Yrs, 8 Months and 10 Days
注意:
-它仅适用于适当的DateTime
类型,不适用于看起来像日期的字符串。使用文字(#...#
)或DateTime
变量。使用a时DateTimePicker
,请.Value
不要使用.Text
。
-它不会做负值,所以传递的值顺序无关紧要
-在计算时,它会增加一个DateTime
变量,因此,它应适当考虑leap日
这是基于我很久以前在博客中找到的一些C#代码(甚至可能来自SO答案或问题)。不能立即找到引用的原始内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句