在vb.net中按年,月和日计算年龄 [英] calculating of age by year,months and day in vb.net

查看:534
本文介绍了在vb.net中按年,月和日计算年龄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是visual basic .net 2008的初学者我想问一些事情..



i有一个3组合框,适用于月,日结果的年份和三个文本框以及计算的1个按钮。这一年是从1950年到2012年



我有一些代码,但我的编码很硬,因为你必须逐一把它...任何人都可以帮忙吗?谢谢你和上帝保佑。



这里是我的示例代码..



Hello, i am a beginner in visual basic .net 2008 i want to ask something..

i have a 3 combo box which is intended for Month,Day and Year and three textboxes for the result and 1 button for the compute. the year is from 1950 upto 2012

i have some code but i is very hard coded because you must put it one by one.. anyone can help? thank you and God bless.

here is my sample code..

 If ComboBox1.SelectedItem = "January" And ComboBox2.SelectedItem = "1" And ComboBox3.SelectedItem = "1950" Then
           
            TextBox1.Text = Val(29) - 1
            TextBox2.Text = (7)-1
            TextBox3.Text = Val(2012) - 1950

end if





这是ou tput是62岁28天6个月..

i想要简单而简短..

end sub







如何让我的代码变短..



which is the output is 62 years old 28 days and 6 months..
i want to make it simple and short..
end sub



how can i make my code short..

推荐答案

是的,似乎它确实像那样工作。这是我快速拼凑的东西。

你想要查看DateTime和TimeSpan的帮助



Yup, seems that it does work like that. Here''s something I threw together quickly.
You''ll want to checkout the help for DateTime and TimeSpan

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim format As String = "ddd d/MMM yyy HH:mm"
    Dim curTime As DateTime = DateTime.Now
    Dim birthDate As New System.DateTime(1900, 8, 8)
    Dim elapsed As TimeSpan


    TextBox1.Text = curTime.ToString(format)
    TextBox2.Text = birthDate.ToString(format)

    elapsed = curTime - birthDate

    TextBox3.Text = Int(elapsed.Days / 365.25)
End Sub





< b>显示器:

Wed 1 / Aug 2012 15:53

Wed 8 / Aug 1900 00:00

111



Displays:
Wed 1/Aug 2012 15:53
Wed 8/Aug 1900 00:00
111


尝试这个。

try this one.
Sub calculateAge(ByVal birthDate As Date)
Dim mySpan As Int16 = CInt(Date.Now.Subtract(birthDate).TotalDays)
Dim nowYear As Int16 = Date.Now.Year
Dim nowMonth As Int16 = Date.Now.Month
Dim birthYear As Int16 = birthDate.Year
Dim birthMonth As Int16 = birthDate.Month

Dim yearCount As Int16
For yearCount = Date.Now.Year To birthYear Step -1
If yearCount Mod 4 = 0 Then
Select Case True
Case yearCount = nowYear And nowMonth < 3
'This is a leap year but we're not past Feb yet
'Do nothing
Case yearCount = birthYear And birthMonth > 2
'They were born after Feb in a leap year
'Do nothing
Case Else
'This was a full leap year, subtract a day to make it 365
mySpan -= 1
End Select
End If
Next

Dim myYears As Int16 = mySpan / 365
Dim myDays As Int16 = mySpan - (myYears * 365)
If myDays < 0 Then
myYears -= 1
myDays = 365 + myDays ' myDays is negative so this will subtract
End If

MessageBox.Show("You are " & myYears & " years and " & myDays & " days old.")
End Sub


试用此代码...

Try this Code...
Dim dob As DateTime
      dob = New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month,           DateTimePicker1.Value.Day)
       Dim tday As TimeSpan = DateTime.Now.Subtract(dob)
       Dim years As Integer, months As Integer, days As Integer
       months = 12 * (DateTime.Now.Year - dob.Year) + (DateTime.Now.Month - dob.Month)

       If DateTime.Now.Day < dob.Day Then
           months -= 1
           days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + DateTime.Now.Day
       Else
           days = DateTime.Now.Day - dob.Day
       End If
       years = Math.Floor(months / 12)
       months -= years * 12
       MsgBox("Your age as on " & Format(Now, "dd-MMM-yyyy") & vbCrLf & years & " Years, " & months & " Months and " & days & " Days")


这篇关于在vb.net中按年,月和日计算年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆