请问我如何计算Vb中的年龄。 Visual Studio 15。 [英] Please How Do I Calculate For Age In Vb. Visual Studio 15.
本文介绍了请问我如何计算Vb中的年龄。 Visual Studio 15。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将dobtxbx文本框中的出生日期减去datetimepicker值,以便我在agetxbx文本框中获得年龄。
agetxbx.Text = Date.Now.Year - dobtxbx.text
解决方案
你不能从DateTime中减去一个字符串。
尝试:
Dim dob As Datetime = DateTime.Parse(dobtxbx.Text)
Dim delta As TimeSpan = DateTime.Today - dob
Dim age As Double = Math.Floor(delta.TotalDays / 365 。 25 )
agetxbx.Text = age.ToString()
希望这会有所帮助。
更正代码以计算年数天数。
我建议阅读 Charles Book Zero,Charles Petzold [ ^ ]。它解释了为什么这不起作用,以及你需要做什么。它是.NET和C#的一个很好的入门。
请参阅MSDN: https://msdn.microsoft.com/en-us/library/b5xbyt6f(v = vs.90).aspx [ ^ ]
i am trying to subtract the date of birth which is is in the dobtxbx textbox as datetimepicker value so that i get the age in the agetxbx textbox.
agetxbx.Text = Date.Now.Year - dobtxbx.text解决方案You cannot subtract a string from a DateTime.
Try:
Dim dob As Datetime = DateTime.Parse(dobtxbx.Text) Dim delta As TimeSpan = DateTime.Today - dob Dim age As Double = Math.Floor(delta.TotalDays / 365.25) agetxbx.Text = age.ToString()
Hope this helps.
[Edit] Corrected the code to compute the number of years from the number of days.
I would suggest reading .NET Book Zero, by Charles Petzold[^]. It explains why this will not work, and what you need to do. It is a great primer for .NET and C#.
See MSDN: https://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx[^]
这篇关于请问我如何计算Vb中的年龄。 Visual Studio 15。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文