4

我想将字符串“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
4

2 回答 2

8

怎么样

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")
于 2013-01-11T03:15:29.760 回答
-1

我认为小写 mm 表示分钟,尝试 MM 表示月份。

于 2016-06-16T14:19:28.203 回答