注册 登录
编程论坛 VB6论坛

[求助]如何从文本框中输入出生日期及自动求出当前年龄(岁数)?

qwaszwc 发布于 2007-10-19 22:57, 3021 次点击

1,在text1中输入职工出生日期,格式为"年年年年-月月-日日",中间的短横自动生成.
2.在text2中自动显示出年龄,即多大岁数.
自己研究了一下午也弄出来,请提供帮助.不盛感激

17 回复
#2
三断笛2007-10-19 23:30
Text2.Text = Val(Format(CDate(Format(Now, "YYYY-MM-DD")) - CDate(Text1.Text), "YYYY")) - 1900

测试:
输入:1986-05-01
输出:21

[此贴子已经被作者于2007-10-19 23:31:12编辑过]

#3
缘吇弹2007-10-20 00:56
On Error GoTo st
If Len(Text1) = 8 And Mid(Text1, 5, 1) <> "-" And Mid(Text1, 8, 1) <> "-" Then
Text1 = Left(Text1, 4) + "-" + Mid(Text1, 5, 2) + "-" + Right(Text1, 2)
Text2.Text = Format(CDate(Format(Now, "YYYY-MM-DD")) - CDate(Text1.Text), "YYYY") - 1900
ElseIf Not (Len(Text1) = 10 And Mid(Text1, 5, 1) = "-" And Mid(Text1, 8, 1) = "-") Then
st: MsgBox ("职工出生日期输入有误,请检查更正!")
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
Else
Text1 = Format(CDate(Text1.Text))
Text2.Text = Format(CDate(Format(Now, "YYYY-MM-DD")) - CDate(Text1.Text), "YYYY") - 1900
End If

[此贴子已经被作者于2007-10-20 0:57:56编辑过]

#4
三断笛2007-10-20 01:41
日期格式问题可以使用MonthView控件,保证输入正确格式的日期
#5
qwaszwc2007-10-20 16:35
谢谢楼上两位,我试试.
#6
qwaszwc2007-10-20 16:45
以下是引用缘吇弹在2007-10-20 0:56:47的发言:
On Error GoTo st
If Len(Text1) = 8 And Mid(Text1, 5, 1) <> "-" And Mid(Text1, 8, 1) <> "-" Then
Text1 = Left(Text1, 4) + "-" + Mid(Text1, 5, 2) + "-" + Right(Text1, 2)
Text2.Text = Format(CDate(Format(Now, "YYYY-MM-DD")) - CDate(Text1.Text), "YYYY") - 1900
ElseIf Not (Len(Text1) = 10 And Mid(Text1, 5, 1) = "-" And Mid(Text1, 8, 1) = "-") Then
st: MsgBox ("职工出生日期输入有误,请检查更正!")
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
Else
Text1 = Format(CDate(Text1.Text))
Text2.Text = Format(CDate(Format(Now, "YYYY-MM-DD")) - CDate(Text1.Text), "YYYY") - 1900
End If

三楼版主太厉害了,一时看不明白,回去慢慢研究.
这段代码是在
private sub text1_change()

end sub
过程中吗?
另:CDate()函数没见过,不知什么意思?

#7
缘吇弹2007-10-20 16:53

最好放在按钮单击过程中,因为你放在private sub text1_change()中,每有变化就激发事件,比较麻烦.
CDate()没看过? 那CInt看过没?

#8
hyhhd2007-10-20 20:45
关键是日期型数据可以进行运算。
#9
qwaszwc2007-10-21 12:55
以下是引用缘吇弹在2007-10-20 16:53:57的发言:

最好放在按钮单击过程中,因为你放在private sub text1_change()中,每有变化就激发事件,比较麻烦.
CDate()没看过? 那CInt看过没?

关键是text1文本框的输入过程,我在其他应用程序中见过很多,都是直接输入如:"19780512",显示"1978-05-12",不可能再按其他按钮的.
我的教材上只有Date()函数"返回系统当前日期",Int()函数"正数取整",不知前面加上"C"什么意思?

#10
刨子头2007-10-21 13:04
转换!
#11
缘吇弹2007-10-21 14:29
以下是引用qwaszwc在2007-10-21 12:55:49的发言:

关键是text1文本框的输入过程,我在其他应用程序中见过很多,都是直接输入如:"19780512",显示"1978-05-12",不可能再按其他按钮的.
我的教材上只有Date()函数"返回系统当前日期",Int()函数"正数取整",不知前面加上"C"什么意思?

既然你要那种类\效果,那你就放在private sub text1_change()里咯.
我那代码只是个参考,具体怎么用你自己抓主意.

#12
那边有朵蘑菇云2007-10-22 12:00
同意LS上的代码
哇哈哈
#13
qwaszwc2007-10-23 07:19
放在private sub text1_change()里确实麻烦.
最好是放在text1_lostFocus()里,上述代码极具建设性.确实可行,但不太直观.不知有没有占位符之类的东西可以解决?
#14
心中有剑2007-10-23 09:22
[CODE]


Const conDateInterval = "/"
Const conDateFormat = "yyyymmdd"
Const conDateFormat1 = "####/##/##"


Private Sub Form_Load()
Text1 = ""
Text2 = 0
Text1.MaxLength = 10
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text2.SetFocus
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack And KeyAscii <> 47 And KeyAscii <> 45 Or KeyAscii = vbKeyReturn Then KeyAscii = 0
End If
End Sub

Private Sub Text1_LostFocus()
If IsDate(ChangeDateFormat(Text1)) Then
Text1 = ChangeDateFormat(Text1)
Text2 = DateDiff("yyyy", Text1, Date)
Else
MsgBox "请输入正确的日期格式"
Text1.SetFocus
End If
End Sub
Private Function ChangeDateFormat(ByVal lsInputDate As String) As String
If Len(lsInputDate) >= 8 Then
If Len(lsInputDate) = 8 Then
lsInputDate = Left(lsInputDate, 4) & conDateInterval & Mid(lsInputDate, 5, 2) & conDateInterval & Right(lsInputDate, 2)
End If
If Len(lsInputDate) = 10 Then
If IsDate(lsInputDate) Then
If Format(lsInputDate, conDateFormat) <= Format(Date, conDateFormat) Then
ChangeDateFormat = Format(Format(lsInputDate, conDateFormat), conDateFormat1)
Else
ChangeDateFormat = lsInputDate
End If
Else
ChangeDateFormat = lsInputDate
End If
End If
Else
ChangeDateFormat = lsInputDate
End If
End Function

[/CODE]
#15
xlin1033xl2007-10-23 14:29
使用vb自带的DTPicker控件
年数=(date-DTPicker.value)/365
这里当然会有误差,假设某人能活100岁,其中闰年的年份不超过100,也就是四个月,也就是不会超过1年,可以忽略不计
#16
qwaszwc2007-10-23 18:35
效果差不多,看来也只能这样了.
#17
qwaszwc2007-10-23 18:51
Private Sub Text1_Change()
If Len(Text1.Text) = 4 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = Len(Text1.Text)
End If
If Len(Text1.Text) = 7 Then
Text1.Text = Text1.Text & "-"
Text1.SelStart = Len(Text1.Text)
End If
End Sub
放上这段代码就完美了.
#18
王俊GXB2014-10-08 10:27
不懂  我只是一个初学者  看哪个东西一点都看不懂
1