为什么这个CDATE格式不能mm / yyyy?

时间:2013-01-11 02:54:59

标签: excel excel-vba vba

我想将字符串“mm / dd / yy hh:mm AM / PM”转换为日期“mm / yyyy”

为什么以下代码输出11-2-2015

Sub Test()

    Dim yourStringDate As String
    Dim yourDateVariable As Date

    yourStringDate = "11/2/15 12:00 AM"        
    yourDateVariable = Format(CDate(yourStringDate), "mm/yyyy")        
    MsgBox yourDateVariable

End Sub

2 个答案:

答案 0 :(得分:7)

怎么样

Dim yourStringDate As Date
yourStringDate = DateValue("11/2/15 12:00 AM")
MsgBox Format(yourStringDate, "mm/yyyy")

或采用原始格式

Dim yourStringDate As String
Dim yourDateVariable As Date
yourStringDate = "11/2/15 12:00 AM"
yourDateVariable = CDate(yourStringDate)
MsgBox Format(yourDateVariable, "mm/yyyy")

答案 1 :(得分:-2)

我认为小写mm是几分钟,试试MM一个月。