469,620 Members | 1,857 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,620 developers. It's quick & easy.

Converting into DateTime

Is there better way to convert integer type date into DateTime type date
as doing like code below?

Dim intDate As Integer = 20051019

Dim dte As DateTime = New DateTime( _
CType(intDate.ToString.Substring(0, 4), Integer), _
CType(intDate.ToString.Substring(4, 2), Integer), _
CType(intDate.ToString.Substring(6, 2), Integer))

--
Thanks in advance!

Mika
Nov 21 '05 #1
8 2292
"Mika M" <ma***************@luukku.com> schrieb:
Is there better way to convert integer type date into DateTime type date
as doing like code below?

Dim intDate As Integer = 20051019
I am curious why you are storing a /date/ as an integer. This is very
uncommon.
Dim dte As DateTime = New DateTime( _
CType(intDate.ToString.Substring(0, 4), Integer), _
CType(intDate.ToString.Substring(4, 2), Integer), _
CType(intDate.ToString.Substring(6, 2), Integer))


\\\
Dim dt As Date = Date.ParseExact(CStr(intDate), "yyyyMMdd", Nothing)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
Is there better way to convert integer type date into DateTime type date
as doing like code below?


You can avoid the string allocations by doing something like this:

Dim dte As DateTime = New DateTime(intDate \ 10000, (intDate Mod 10000) \
100, intDate Mod 100))
Mattias

Nov 21 '05 #3
Mika,

I would probably do

Dim srtDate as String = 20051019.ToString

Dim dte As DateTime = New DateTime( _
Cint(strDate.Substring(0, 4)), Cint(strDate.Substring(4, 2)), _
Cint(strDate.Substring(6, 2))))

It is probably some parts of a nanoseconds faster however probably the same.

Cor
Nov 21 '05 #4
Herfried,

Better than my sample in this thread, (and I knew this one) donk donk.

:-)

Cor
Nov 21 '05 #5
Herfried K. Wagner [MVP] kirjoitti:
I am curious why you are storing a /date/ as an integer. This is very
uncommon.


I'm reading contents of CSV-file rows into DataTable. CSV-file contains
date values as strings that way, and I try to convert them into
DataTable's datetime field.
Nov 21 '05 #6
"Mika M" <ma***************@luukku.com> schrieb:
I am curious why you are storing a /date/ as an integer. This is very
uncommon.


I'm reading contents of CSV-file rows into DataTable. CSV-file contains
date values as strings that way, and I try to convert them into
DataTable's datetime field.


If you already have the values in string format,
'Date.Parse'/'Date.ParseExact' is the way to go. You can skip the step of
converting the string to an integer.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
Herfried,
| I am curious why you are storing a /date/ as an integer. This is very
| uncommon.
Its common to store a "date" as an "integer" on AS/400 databases & other
databases.

At least at the shops that have code pre-dating "date" fields being added to
their database systems.

I normally seen the dates stored as Decimal(9, 0) or Packed(9,0) rather then
an Integer (32-bit binary value).

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
| "Mika M" <ma***************@luukku.com> schrieb:
| > Is there better way to convert integer type date into DateTime type date
| > as doing like code below?
| >
| > Dim intDate As Integer = 20051019
|
| I am curious why you are storing a /date/ as an integer. This is very
| uncommon.
|
| > Dim dte As DateTime = New DateTime( _
| > CType(intDate.ToString.Substring(0, 4), Integer), _
| > CType(intDate.ToString.Substring(4, 2), Integer), _
| > CType(intDate.ToString.Substring(6, 2), Integer))
|
| \\\
| Dim dt As Date = Date.ParseExact(CStr(intDate), "yyyyMMdd", Nothing)
| ///
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
Nov 21 '05 #8
Herfried K. Wagner [MVP] wrote:
I'm reading contents of CSV-file rows into DataTable. CSV-file
contains date values as strings that way, and I try to convert them
into DataTable's datetime field.

If you already have the values in string format,
'Date.Parse'/'Date.ParseExact' is the way to go. You can skip the step
of converting the string to an integer.


Yes I did it directly from string to datetime - just asked question
differently using integer :)

Well... Now I'm wondering is it even possible to transfer those string
type days (like "20051020") to datetime format already at the same time
when reading CSV-file into DataTable-object using Provider
Microsoft.Jet.OLEDB.4.0 ?

--
Mika
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by M. Uppal | last post: by
3 posts views Thread by NateM | last post: by
6 posts views Thread by minboymike | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.