获取年,月,对经过的时间跨度日(日期时间) [英] Get years, months, days for an elapsed span of time (DateTime)
问题描述
我如何可以显示年龄为年,月,日的日期时间选择器值。例如:
How can I display an age as Years, Months, Days from a datetime picker value. Eg:
Datetimepicker value = #1/11/2014#
Today = #1/12/2015#.
最终的结果将是1年,0月,1天(S)。但是,得到的结果不仅仅是减去 DateTime.Year
值等等。是否有人知道一个数学公式或数学计算的?
The final result would be "1 Year, 0 Months, 1Day(S)". But getting that result is more than just subtracting the DateTime.Year
values and so forth. Does anybody know a mathematics formula or mathematics calculation about that?
推荐答案
下面是一个 DateTimeSpan
键入返回像时间跨度对象code>重presenting的 - 22岁,月,日两个日期之间经过。它环路直通的时间单位递增,直到较小的日期相匹配的大。由此产生的增量然后返回一个新的
DateTimeSpan
对象。
Here is a DateTimeSpan
Type which returns an object like a TimeSpan
representing the Yrs, Months, Days elapsed between 2 dates. It loops thru the time units to increment them until the smaller date matches the larger. The resulting increments are then returned in a new DateTimeSpan
object.
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年,8月10日
注:
- 它只能在适当的的DateTime
类型,而不是看起来像日期字符串。用文字(#...#
)或者的DateTime
变量。当使用的DateTimePicker
,使用。价值
不是。文
。照片
- 它没有做负值,所以值传递的顺序并不重要
- 当计算,它增加一个的DateTime
变量,因此,应适当考虑闰日
Notes:
- It works only on proper DateTime
types, not strings which look like dates. Use literals (#...#
) or DateTime
variables. When using a DateTimePicker
, use .Value
not .Text
.
- It does not do negative values, so the order of values passed does not matter
- When calculating, it increments a DateTime
variable so, it should properly account for leap days
这是基于一些C#code我在博客(或者甚至从SO回答或问题)发现很久以前。不能找到它刚才举的原件。
This is based on some C# code I found long ago in a blog (or perhaps even from an SO answer or question). Cant find it just now to cite the original.
这篇关于获取年,月,对经过的时间跨度日(日期时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!