user1077685
user1077685

Reputation:

Why won't this CDATE format to mm/yyyy?

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

Answers (2)

jawaad farooqui
jawaad farooqui

Reputation: 11

I think lower case mm is for minutes, try MM for month.

Upvotes: -1

brettdj
brettdj

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

Related Questions