我并不需要返回日期格式,只要返回字符串:03年05月尝试了下Format("3年5月","00\年00\月")也不行

解决方案 »

  1.   

    Dim s As String
        s = Format("3年5月", "YY年MM月")
      

  2.   

    format("3年5月","yy年mm月")
    03年05月
      

  3.   

    楼上两位出手真快,再问:如果是format("3年15月","yy年mm月"),返回是3年15月因为这个根本不是日期格式,我只要字符串,这种情况不得不考虑
      

  4.   

    如果 你是想返回 “03年15月”Option ExplicitPrivate Sub Command1_Click()
        Dim s As String
        s = "3年15月"
        s = a(s)
        MsgBox s
    End SubPrivate Function a(s As String) As String
        Dim i As Integer
        Dim s1 As String, s2 As String
        i = InStr(1, s, "年")
        If i > 0 Then
            s1 = Mid(s, 1, i - 1)
            If Len(s1) = 1 Then s1 = "0" & s1
                
            s2 = Mid(s, i + 1)
            i = InStr(1, s2, "月")
            If i > 0 Then
                s2 = Mid(s2, 1, i - 1)
                If Len(s2) = 1 Then s2 = "0" & s2
            End If
            
            a = s1 & "年" & s2 & "月"
        End If
    End Function
      

  5.   

    format("03年15月","yy年mm月")
    03年15月
      

  6.   

    其实15月已经超出mm的范围(00-12)
    format("03年15月")
    也是03年15月
      

  7.   

    先写成日期格式,然后用Mid函数截取成字符串
      

  8.   

    strDate = "03年15月"strDate = IIf(Isdate(strDate), Format(strDate, "yy年MM月"), "非法日期")