Converting into DateTime

M

Mika M

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))
 
C

Cor Ligthert [MVP]

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
 
H

Herfried K. Wagner [MVP]

Mika M said:
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)
///
 
G

Guest

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
 
C

Cor Ligthert [MVP]

Herfried,

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

:)

Cor
 
M

Mika M

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.
 
H

Herfried K. Wagner [MVP]

Mika M said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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

| > 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/>
|
 
M

Mika M

Herfried said:
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top