Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm converting a datatable into a csv file format. the data contains 3 date columns. What happens is that the dates becomes like this "40939" in the converted csv file. In the data, its "12/15/2012" but after conversion it becomes "40939". Please help me on how to do it in the date format.
Posted
Comments
Garth J Lancaster 18-Dec-12 22:05pm    
you say 'after conversion' - what is the code you are using ? surely what you would be doing is something like

String aDateString = dt.Rows[x][y].ToString("MM/dd/yyyy")

where dt is your datatable, x is a valid row index and y is the column index for a date column defined as datatype typeof(DateTime)

{Whoops - I see you're using VB.NET - you may need to Modify This, but thats the general idea}
Alan Tuscano 19-Dec-12 0:43am    
Hi Garth,

I may have found the root of the problem.

the original file is from excel and is converted into datatable. see code below:


Dim sr As New StreamReader(path)

Dim fullFileStr As String = sr.ReadToEnd()

sr.Close()

sr.Dispose()

Dim lines As String() = fullFileStr.Split(ControlChars.Lf)

Dim recs As New DataTable()

Dim sArr As String() = lines(0).Split("~"c)

For Each s As String In sArr

recs.Columns.Add(New DataColumn())

Next

'recs.Columns.Add(New DataColumn())


Dim row As DataRow

Dim finalLine As String = ""

For Each line As String In lines

row = recs.NewRow()

finalLine = line.Replace(Convert.ToString(ControlChars.Cr), "")

row.ItemArray = finalLine.Split("~"c)

recs.Rows.Add(row)

Next

from this process, the date value is converted in numeric 40939 from its original excel format of 20120131.

How can I convert the date column to date format when converting into datatable using the same code above?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900