I have a string containing a short date and a year, e.g. dec2013. The format is always fixed but the month is always lower case. I need to convert this to a date so I can loop through some months. The day is not important; it would be fine if it defaulted to the first of the month. Googling has led me to things like DateTime.ParseExact but I can't seem to make it work. This was one try:
I tried converting the first letter to upper case but it didn't help. Also tried:
This gives the error "String was not recognized as a valid DateTime" even if I include a day and capitalise the month e.g. 01Dec2013 and add the corresponding dd to the date format string.
Where am I going wrong?
Code:
Imports System.Globalization
Dim sCurrent As String = "dec2013"
Dim dte As Date
If DateTime.TryParseExact(sCurrent, "MMMYYYY", New CultureInfo("en-GB"), DateTimeStyles.None, dte) Then
MsgBox(dte.ToString)
Else
MsgBox("Date error ...")
End IfCode:
Dim dte As DateTime = DateTime.ParseExact(sCurrent, "MMMYYY", CultureInfo.CurrentCulture)Where am I going wrong?