I’m trying to convert a string into a date format
My string looks like this
Dim MyString as String = "June 2011"
And I’m trying to convert it like this
Convert.ToDateTime(MyString).ToString("MM yyyy")
But it’s giving me the error
Syntax error converting datetime from
character string.
The desired output would be either 06/2011 or 01/06/2011. The value of my string will only ever have the month and year in though.
Any ideas?
A datetime object in .NET represents one single point in time. To create one from a Gregorian calendar date, you need all three parts (day, month, year), otherwise it can’t represent one single point in time.
Since your business requirement is to assume that the day number is 1 if not provided, just insert that number into the string before parsing.
EDIT:
Sorry, forgot to mention that string manipulations like that are of course culture-dependent. You don’t mention what culture you are in.
"June 2011"can imply eitheren-GBoren-US. Since inserting the day number at the start of the string is easier than trying to insert it between the month and year, I suggest you go for something like this.