参考Format SET DATE [TO] AMERICAN | ANSI | BRITISH | FRENCH | GERMAN | ITALIAN | JAPAN | TAIWAN | USA | MDY | DMY | YMD | SHORT | LONG其中ITALIAN代表dd-mm-yy
一个笨方法,不过还有效 Private Sub Command1_Click()Dim strtemp As String Dim a As Stringa = Mid$(Text1.Text, 1, 4) + "-" strtemp = a a = Mid$(Text1.Text, 5, 2) + "-" strtemp = strtemp + a a = Mid$(Text1.Text, 7, 2) strtemp = strtemp + a MsgBox strtemp End Sub
Private Sub Text1_Change() Dim strTemp As String If Len(Text1.Text) < 8 Then Exit Sub strTemp = Mid(Text1.Text, 1, 4) & "-" & Mid(Text1.Text, 5, 2) & "-" & Mid(Text1.Text, 7, 2) If IsDate(strTemp) Then Text1.Text = strTemp Text1.SelStart = Len(Text1.Text) End Sub
写错了 Option ExplicitPrivate Sub Text1_Change() Dim strTemp As String If Len(Text1.Text) < 8 Then Exit Sub strTemp = Mid(Text1.Text, 1, 4) & "-" & Mid(Text1.Text, 5, 2) & "-" & Mid(Text1.Text, 7, 2) If IsDate(strTemp) Then Text1.Text = strTemp Text1.SelStart = Len(Text1.Text) End If End Sub
sorry我没有注意,海军上校的做法是对的我的办法必须是这样的字符2002-01-01sorry
to 小山兄 是阿,我最开始就使用format 不过发现好像行不通,采用哪个笨方法,不知道有没有更好的方法实现
2.通过程序实现
比如在lostfocus事件中添加代码
3.自己做一个控件来实现
SET DATE [TO] AMERICAN | ANSI | BRITISH | FRENCH | GERMAN
| ITALIAN | JAPAN | TAIWAN | USA | MDY | DMY | YMD
| SHORT | LONG其中ITALIAN代表dd-mm-yy
Private Sub Command1_Click()Dim strtemp As String
Dim a As Stringa = Mid$(Text1.Text, 1, 4) + "-"
strtemp = a
a = Mid$(Text1.Text, 5, 2) + "-"
strtemp = strtemp + a
a = Mid$(Text1.Text, 7, 2)
strtemp = strtemp + a
MsgBox strtemp
End Sub
Dim strTemp As String
If Len(Text1.Text) < 8 Then Exit Sub
strTemp = Mid(Text1.Text, 1, 4) & "-" & Mid(Text1.Text, 5, 2) & "-" & Mid(Text1.Text, 7, 2)
If IsDate(strTemp) Then Text1.Text = strTemp
Text1.SelStart = Len(Text1.Text)
End Sub
Option ExplicitPrivate Sub Text1_Change()
Dim strTemp As String
If Len(Text1.Text) < 8 Then Exit Sub
strTemp = Mid(Text1.Text, 1, 4) & "-" & Mid(Text1.Text, 5, 2) & "-" & Mid(Text1.Text, 7, 2)
If IsDate(strTemp) Then
Text1.Text = strTemp
Text1.SelStart = Len(Text1.Text)
End If
End Sub
是阿,我最开始就使用format
不过发现好像行不通,采用哪个笨方法,不知道有没有更好的方法实现
我的email是[email protected]
楼主:我给各思路1:判断是否是数值(如20020101)
2:判断长度(yymmdd or yyyymmdd)
3:截取字符,构成日期