Dim str As String = 20020206
        If str.Length = "8" Then
            Dim str1 As String = str.Substring(4, 8)
            Me.Label1.Text = str1
        End If
其他信息: 索引和长度必须引用该字符串内的位置。
        Dim str As String = 20020206
        If str.Length = "8" Then
            Dim str1 As String = str.Substring(0, 4) & "/" & str.Substring(4, 6) & "/" & str.Substring(6)
            Me.Label1.Text = str1
        End If
其他信息: 索引和长度必须引用该字符串内的位置。
为什么不能插入啊  我想把20020206变成 2002/02/06

解决方案 »

  1.   


    (1)标准模块中自定义函数'函数名称:GetLastDateInMonth
    '编写时间:2004-08-26
    '最后更新:2004-08-26
    '编写人:
    '功能:某月的最后一天是多少号Public Function GetLastDateInMonth(ByVal MyDate As Date) As Integer
    On Error Resume Next    GetLastDateInMonth = (31 - Day(DateSerial(Year(MyDate), Month(MyDate), 31)))
        If GetLastDateInMonth = 0 Then GetLastDateInMonth = 31
        
    End Function
    '函数名称:CheckDate
    '编写时间:2004-08-26
    '最后更新:2004-08-26
    '编写人:
    '功能:检查日期格式是否合法,如果不合法,是哪一部分出错Public Function CheckDate(ByVal split As String, _
                              ByVal s As String) As Boolean
    'split 为日期格式分隔符
    's为传入的待判断的字符串    Dim dd As Date, i As Integer
        Dim strDate As String
        
        If Len(s) <> 8 Then
            MsgBox "请输入8位数字!", vbExclamation + vbOKOnly
            Exit Function
        End If
        
        For i = 1 To Len(s)
            If Asc(Mid(s, i, 1)) > 57 Or Asc(Mid(s, i, 1)) < 48 Then
                MsgBox "您输入的日期字符中的[" & Mid(s, i, 1) & "]不是数字!"
                Exit Function
            End If
        Next
        
        strDate = Mid(s, 1, 4) & split & Mid(s, 5, 2) & split & Mid(s, 7, 2)
        
        If Not IsDate(strDate) Then
            If CLng(Mid(s, 5, 2)) > 12 Then
                MsgBox "月份不可大于12", vbExclamation + vbOKOnly
                Exit Function
            End If
            
            dd = DateSerial(Mid(s, 1, 4), Mid(s, 5, 2), 1)
            i = GetLastDateInMonth(dd)
            
            If CLng(Mid(s, 7, 2)) > i Then
                MsgBox "日期不可大于" & i, vbExclamation + vbOKOnly
                Exit Function
            End If
        End If
        
        CheckDate = TrueEnd Function(2)窗体引用时,例:
    Private Sub temp_Click()Dim str As Stringstr = InputBox("请输入代表日期的数字")
    CheckDate "/", trim(str)End Sub
    ---------------------------------------------------注:If Asc(Mid(s, i, 1)) > 57 Or Asc(Mid(s, i, 1)) < 48 Then ……
    这里之所以不用IsNumeric函数,是因为IsNumeric(3e5)也为真.