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?
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
You could do something like this;
Code:
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
 

Jose Cuervo

Member
Joined
Jul 14, 2010
Messages
14
Programming Experience
1-3
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 :)
 

MattP

Well-known member
Joined
Feb 29, 2008
Messages
1,206
Location
WY, USA
Programming Experience
5-10
DateTime.ParseExact has an overload accepting a string array of expected input formats and the culture info.

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