![](/img/trans.png)
[英]Replace empty Day or Month or Year with Current Day or Month or Year in masked textbox in vb.net
[英]how to calculate birthdate from age (day , month ,year) and current date in vb.net
如何根據年齡(日、月、年)和當前日期計算出生日期我試過了,但結果顯示不正確我可以從出生日期正確獲取年齡,但從年齡獲取出生日期,計算時結果與原始出生日期樣本不同2022 年 4 月 15 日的年齡,然后計算這個年齡的出生日期,它出現在 2022 年 12 月 4 日
Dim day, month, year As Integer
day = CInt(NumDay.Value)
month = CInt(NumMonth.Value)
year = CInt(NumYear.Value)
Dim dateResult As Date = Now
DateTimePicker1.Value = dateResult.AddYears(-year).AddMonths(-month).AddDays(-day)
在此處輸入代碼
Function AgeCalculator(ByVal FromDate As Date, Todate As Date, ByRef year As Integer, ByRef month As Integer, ByRef day As Integer, Optional flgyearOnly As Boolean = False) As String '23/11/2017
If Not IsDate(FromDate) Then Return ""
Dim tmpYear As String = "", tmpMonth As String = ""
Dim tmpdiffDaya As Integer = 0
Dim tmpAge As String = ""
tmpdiffDaya = CInt(DateDiff(DateInterval.Day, (FromDate), Todate))
If tmpdiffDaya <= 0 Then Return ""
If tmpdiffDaya > 0 And tmpdiffDaya <= 29 Then
Return tmpdiffDaya & " Days"
End If
If tmpdiffDaya = 30 Then
Return " 1 Month"
End If
tmpYear = CStr(tmpdiffDaya / 365)
If InStr(tmpYear, ".") > 0 Then
tmpYear = Microsoft.VisualBasic.Left(tmpYear, InStr(tmpYear, ".") - 1)
End If
year = CInt(tmpYear)
If Val(tmpYear) = 0 Then tmpYear = ""
If Val(tmpYear) > 0 Then
tmpAge = tmpYear & " years"
If flgyearOnly Then Return tmpAge
End If
tmpdiffDaya = CInt((tmpdiffDaya - (Val(tmpYear) * 365)))
tmpMonth = CStr(tmpdiffDaya / 30)
If InStr(tmpMonth, ".") > 0 Then
tmpMonth = Microsoft.VisualBasic.Left(tmpMonth, InStr(tmpMonth, ".") - 1)
End If
month = CInt(tmpMonth)
If Val(tmpMonth) = 0 Then
tmpMonth = ""
Else
If CInt(tmpMonth) > 0 Then
tmpAge &= " " & tmpMonth & " Months"
End If
End If
tmpdiffDaya = CInt((tmpdiffDaya - (Val(tmpMonth) * 30)))
day = tmpdiffDaya
If Val(tmpdiffDaya) > 0 Then
tmpAge &= " " & tmpdiffDaya & " Days"
End If
Return tmpAge
End Function
您需要先調用AddDays
,然后AddMonths
,然后AddYears
。 如果您想以年、月和日來計算一個人的年齡,那么您將首先計算年,然后是月,然后是日。 如果您以另一種方式進行計算,則實際上必須以另一種方式進行。 如果你先減去年份,那么閏年可能會拋出計算。 如果您在幾天前減去幾個月,那么該計算將受到每個月不同天數的影響。
Private Function GetPastDate(years As Integer, months As Integer, days As Integer) As Date
Return Date.Today.AddDays(-days).AddMonths(-months).AddYears(-years)
End Function
我的出生日期是 1969 年 6 月 19 日,這意味着我今天(2022 年 8 月 4 日)53 歲零 1 個月零 16 天。 如果我對這些數字使用上述方法,那么我會得到正確的出生日期。 如果我以相反的方式調用這些方法,我會得到 6 月 18 日而不是 19 日。
編輯:
我沒有仔細看,但您的AgeCalculator
似乎不起作用。 我剛剛測試了以下代碼:
Module Module1
Sub Main()
Dim dateOfBirth = #4/15/2012#
Dim years As Integer
Dim months As Integer
Dim days As Integer
CalculateAge(dateOfBirth, years, months, days)
Dim output = GetPastDate(years, months, days)
End Sub
Private Sub CalculateAge(dateOfBirth As Date, ByRef years As Integer, ByRef months As Integer, ByRef days As Integer)
Dim temp As Date
Dim currentDate = Date.Today
dateOfBirth = dateOfBirth.Date
years = 0
months = 0
Do
temp = dateOfBirth.AddYears(1)
If temp > currentDate Then
Exit Do
End If
years += 1
dateOfBirth = temp
Loop
Do
temp = dateOfBirth.AddMonths(1)
If temp > currentDate Then
Exit Do
End If
months += 1
dateOfBirth = temp
Loop
days = (currentDate - dateOfBirth).Days
End Sub
Private Function GetPastDate(years As Integer, months As Integer, days As Integer) As Date
Return Date.Today.AddDays(-days).AddMonths(-months).AddYears(-years)
End Function
End Module
它按預期工作。 years
、 months
、 days
的值分別為 10、3 和 20, output
的值為#4/15/2012#
,與初始輸入相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.