Link to home
Start Free TrialLog in
Avatar of yarlapati
yarlapati

asked on

DateTime conversion

Hi,

I am trying to load data from a text file and one of the column is Date string of format ... "20050331".

I have a class which reads a line from the file and returns a StringCollection. ANd then I make a new System.Data.DataRow with all these strings and add it to the DataTable in which the Column dataTypes are defined and then add the row to the Table.

These are the lines of code which does the above...
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
RowStrings is a String Collection...
NewRow is DataRow..

For Each RowString As String In RowStrings
                    Try
                       
                        NewRow(Count) = System.Convert.ChangeType(RowString, IndexedTable.Columns(Count).DataType)
                    Catch EX As Exception
                        ' Catch the exception
                    End Try

                    Count = Count + 1
 Next
IndexedTable.Rows.Add(NewRow)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
But when it reads the string "20050331"  I get an exception as " String was not recognized as a valid DateTime".

I am not sure how to convert these strings to Datetime as the above class is very generic to rad any file with any number of columns....

Can someone give a n idea of how to read these strings as DateTime...

Thanks in advance..

K.
Avatar of entrapnet
entrapnet

you need to set your string as follow into to turn into datatetime

1) mm/dd/yyyy
2) dd-mmm-yyyy (1-Jan-2004)
3) yyyy-mm-dd (2004-1-1)
*datatetime = datetime
base on ur data... i do recommended that you wrote a function like below

        Dim a As DateTime
        Dim b As String

        b = "20050331"
        a = Left(b, 4) + "-" + Mid(b, 5, 2) + "-" + Right(b, 2)
ASKER CERTIFIED SOLUTION
Avatar of heintalus
heintalus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial