简体   繁体   English

从文本框 dob 计算年龄

[英]calculate age from textbox dob

"i have userform containing two textboxes TextBox1 belong to date of birth and textbox2 belong to age.How create code when enter date of birth in TextBox1 then show complete age and month in TextBox2?" “我有包含两个文本框 TextBox1 属于出生日期和 textbox2 属于年龄的用户窗体。如何在 TextBox1 中输入出生日期然后在 TextBox2 中显示完整的年龄和月份时创建代码?”

Dim Age
Dim Birthday As Date
Birthday = CDate(TextBox1.Text)
Age = Year(Date) - Year(Birthday)
TextBox2.Value = Age

i expect output eg 25.10 but actual output 25我预计 output 例如 25.10 但实际 output 25

DateDiff function provides the difference between the two dates in many formats. DateDiff function 以多种格式提供两个日期之间的差异。 Using the Date Diff function we can get the number of months between the dates and convert them into the required format like below使用日期差异 function 我们可以获得日期之间的月数并将它们转换为所需的格式,如下所示

Dim Age
Dim Birthday As Date
Dim years As Integer
Dim months As Integer
Birthday = CDate(TextBox1.Text)
Age = DateDiff("m", (Birthday) , (Date) )
years = Application.WorksheetFunction.RoundDown(Age / 12, 0)
months = Age Mod 12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM