Reputation: 25
The data that I have to convert is written as the separate variables "Month", "Day", and "Year".
The data I need to convert for example is: Month is "July" Day is "21" Year is "2013"
Upvotes: 2
Views: 1125
Reputation: 1229
I would first put the date into a string so
Dim dateString as string = string.format({0} {1} {2}, day, month, year)
Where day, month, year reference the variables holding the date values.
Then try
Dim result as DateTime = Convert.ToDateTime(dateString)
Upvotes: 0
Reputation: 460340
Combine it and use DateTime.ParseExact
with CultureInfo.InvariantCulture
:
Dim dtStr = String.Format("{0} {1} {2}", month, day, year)
Dim dt = Date.ParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture)
Also have a look at: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
If you don't know if the data is valid you should use DateTime.TryParseExact
:
Dim dt As DateTime
If Date.TryParseExact(dtStr, "MMMM dd yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt) Then
Console.Write("Date is: " & dt.ToShortDateString())
End If
Upvotes: 4