Reputation:
I want to convert a string "mm/dd/yy hh:mm AM/PM" to a date "mm/yyyy"
Why is the following code outputting 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
Upvotes: 4
Views: 23755
Reputation: 55692
how about
Dim yourStringDate As Date
yourStringDate = DateValue("11/2/15 12:00 AM")
MsgBox Format(yourStringDate, "mm/yyyy")
or in your original format
Dim yourStringDate As String
Dim yourDateVariable As Date
yourStringDate = "11/2/15 12:00 AM"
yourDateVariable = CDate(yourStringDate)
MsgBox Format(yourDateVariable, "mm/yyyy")
Upvotes: 8