日時ピッカー値から年齢を年、月、日として表示するにはどうすればよいですか。例えば:
Datetimepicker value = #1/11/2014#
Today = #1/12/2015#.
最終的な結果は、「1 年 0 か月 1 日」になります。しかし、その結果を得ることは、単にDateTime.Year
値を減算するなどではありません。それに関する数式や数学の計算を知っている人はいますか?
日時ピッカー値から年齢を年、月、日として表示するにはどうすればよいですか。例えば:
Datetimepicker value = #1/11/2014#
Today = #1/12/2015#.
最終的な結果は、「1 年 0 か月 1 日」になります。しかし、その結果を得ることは、単にDateTime.Year
値を減算するなどではありません。それに関する数式や数学の計算を知っている人はいますか?
これは、 2 つの日付の間に経過した年、月、日を表す のDateTimeSpan
ようなオブジェクトを返す Typeです。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
変数を使用します。を使用する場合は、 notDateTimePicker
を使用してください。
- 負の値を処理しないため、渡される値の順序は重要ではありません
- 計算時に変数をインクリメントするため、うるう日を適切に考慮する必要があります.Value
.Text
DateTime
これは、ずっと前にブログで見つけた C# コードに基づいています (または、SO の回答や質問からでも)。オリジナルを引用するのは今のところ見つかりません。
この方法は非常に単純明快です。
ループとクラス プロパティを作成する必要はありません。
これは、実数Date
型とStrings
日付の両方で機能します。
'Date Picker Value
Dim dBirthDate As Date = "#01/11/2014#"
'Today
Dim dToday As Date = "#01/12/2015#"
Dim dBirthDay As Date
Dim sYears As String
Dim sDays As String
Dim sMonths As String
Dim sResults As String
sYears = DateDiff(DateInterval.Year, dBirthDate, dToday)
dBirthDay = dBirthDate.AddYears(sYears)
sMonths = DateDiff(DateInterval.Month, dBirthDay, dToday)
sDays = DateDiff(DateInterval.Day, dBirthDay, dToday)
sResults = sYears & " Year " & sMonths & " Months " & sDays & " Day(s)"
結果の例: 1 年 0 か月 1 日
完全な結果を得るには、これを追加する必要があります: sDays = (DateDiff(DateInterval.Day, dBirthDay, dToday)) - sMonths * 30