我需要制作一个程序,您可以在其中输入当前日期和用户的出生日期,它可以为您提供用户的年龄(以天为单位)。我已经看过这项工作多年了,但几天和几个月似乎都没有用。非常感谢帮助。
答案 0 :(得分:2)
用户的年龄
您可以使用TimeSpan.Days
Dim span As TimeSpan = currentDate - birthDate
Dim days As Int32 = span.Days
例如我的生日:
Dim currentDate = Date.Now
Dim birthDate = New DateTime(1973, 7, 3)
Dim span As TimeSpan = currentDate - birthDate
Dim days As Int32 = span.Days ' => 14870 omg
答案 1 :(得分:0)
请参阅:A Simple Age Calculator Using VB.Net
代码段:
Public Class Form1
Dim dt1 As Date
Dim dt2 As Date
Dim dt3 As TimeSpan
Dim diff As Double
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
dt1 = Me.DateTimePicker1.Value.ToShortDateString
dt2 = Me.DateTimePicker2.Value.ToShortDateString
dt3 = (dt2 - dt1)
diff = dt3.Days
Me.Label1.Text = ("Your Age is " + Str(Int(diff / 365)) + " Year ")
diff = diff Mod 365
Me.Label1.Text = Me.Label1.Text + (Str(Int(diff / 30)) + " Month(s)")
diff = diff Mod 30
Me.Label1.Text = Me.Label1.Text + (Str(diff) + " Day(s)")
End Sub
End Class
使用TimeSpan.Days
将时间从DOB传递到当前日期,并应用简单的公式来获得所需的结果。