Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27348

VS 2010 [RESOLVED] Get Date from string containing short month and year

$
0
0
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:

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 If

I tried converting the first letter to upper case but it didn't help. Also tried:

Code:

Dim dte As DateTime = DateTime.ParseExact(sCurrent, "MMMYYY", CultureInfo.CurrentCulture)
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?

Viewing all articles
Browse latest Browse all 27348

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>