Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VB.NET
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 15:26pm
Garth J Lancaster at 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 at 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()
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())
'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)
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
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811

Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
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