String to DateTime

  • Thread starter Thread starter koorb
  • Start date Start date
K

koorb

I am trying to read a line from a text file and store it in a DateTime
veritable. Below is a cut down version of my problem code (in my
actual version I am using a try and catch, and I am receiving each
line in a loop to store into the Array)

If I write the string directly into the array it works, but when I use
the line from the file it doesn't go into the DateTime veriable and I
get an error. Is there something I am missing?

Dim inputArray(2) As String
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)
inputArray(2) = LineInput(1)
FileClose(1)

Dim temp As DateTime = CDate(inputArray(2)) 'Problem Line

The line in the file reads:
9/14/2004 6:00:00 PM
And yes I have tired adding #s.
And I even tired Option Explicit Off

The error I get is:
An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "9/14/2004 6:00:00 PM" to
type 'Date' is not valid.

I bet it's something dead simple.
 
The error I get is:
An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "9/14/2004 6:00:00 PM" to
type 'Date' is not valid.


According to the documentation:

CDate recognizes date formats according to the locale setting of your
system. You must provide the day, month, and year in the correct order
for your locale, or the date may not be interpreted correctly.


I see that you're in the UK and the date you're using is in US format.
It could be that CDate is trying to convert the 9th day of the 14th
month into a valid date time - which is an invalid cast exception.

Try changing your computer to US Date Times?
 
According to the documentation:

CDate recognizes date formats according to the locale setting of your
system. You must provide the day, month, and year in the correct order
for your locale, or the date may not be interpreted correctly.


I see that you're in the UK and the date you're using is in US format.
It could be that CDate is trying to convert the 9th day of the 14th
month into a valid date time - which is an invalid cast exception.

Try changing your computer to US Date Times?

Thankyou, it works!
And yet when I input a DateTime veritable directly, I have to use US!
 
Anon,
You are correct that using "/" or "\" doesn't really matter if you follow
the rules & use System.IO.Path to parse your file paths.

However! How many people, incorrectly!, use String.LastIndexOf to find the
last "\" in a path to strip off the path from the file name. How many
people, use String.IndexOf instead of String.LastIndexOf to find "." while
looking for extensions?

Because Win32 allows either "\" or "/" for the path separator, as you
identified, is why I recommend using System.IO.Path to parse file paths
instead of rolling your own...

Note like Herfried I normally use "\", more a force of habit then
anything...

Note I am using String.LastIndexOf to include VB.InStrRev & String.IndexOf
to include VB.InStr.

Just a thought
Jay
 
Back
Top