Question Conversion from String to Date not valid

Jose Cuervo

Member
Joined
Jul 14, 2010
Messages
14
Programming Experience
1-3
Hi, I have a string in the format of 20100730 which i need to convert to UK format of 30/07/2010.

When i try to set 20100730 as a date it says it is not valid. Is there another way i can do this?
 
You could do something like this;
VB.NET:
Dim enteredString As String = "20100730"
Dim yr As String = enteredString.substring(0,4)
Dim mnt As String = enteredString.substring(4,2)
Dim dy As String = enteredString.substring(6,2)
Dim dte as Date = New Date(yr,mnt,dy)

Hope this helps

Satal :D
 
Hi Satal,

Believe it or not, that was the next road i was going down, was just wondering if there was an easier/quicker/tidier way of doing it?

Cheers :)
 
DateTime.ParseExact has an overload accepting a string array of expected input formats and the culture info.

VB.NET:
        Dim enteredString As String = "20100730"
        Dim dt1 As DateTime = DateTime.ParseExact(enteredString,
                                                  New String() {"yyyyMMdd"},
                                                  New CultureInfo("en-US"),
                                                  Globalization.DateTimeStyles.AllowWhiteSpaces)
 
Back
Top