Click here to Skip to main content
12,348,536 members (68,503 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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 18-Dec-12 14:26pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100