我想将字符串“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
最佳答案
怎么样
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")
关于excel - 为什么不将此 CDATE 格式转换为 mm/yyyy?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14270923/