By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,082 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,082 IT Pros & Developers. It's quick & easy.

String to DateTime

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
koorb <ko***@raidrs.co.uk> wrote in
news:sm********************************@4ax.com:
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?


--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2

P: n/a
On Tue, 31 Aug 2004 20:48:14 GMT, Lucas Tam <RE********@rogers.com>
wrote:
koorb <ko***@raidrs.co.uk> wrote in
news:sm********************************@4ax.com :
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?


Thankyou, it works!
And yet when I input a DateTime veritable directly, I have to use US!
Nov 21 '05 #3

P: n/a
* koorb <ko***@raidrs.co.uk> scripsit:
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)


"/" -> "\".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:up**************@TK2MSFTNGP09.phx.gbl:
* koorb <ko***@raidrs.co.uk> scripsit:
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)


"/" -> "\".


\ / doesn't really matter.
Nov 21 '05 #5

P: n/a
Herfried,

"/" -> "\".

This does not give a smily with me, do you know why?

Cor
Nov 21 '05 #6

P: n/a
* "Anon-E-Moose" <an**********@yahoo.com> scripsit:
* koorb <ko***@raidrs.co.uk> scripsit:
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)


"/" -> "\".


\ / doesn't really matter.


.... nevertheless on a Windows platform I would always use "\".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
"/" -> "\".


This does not give a smily with me, do you know why?


LOL!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8

P: n/a
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

"Anon-E-Moose" <an**********@yahoo.com> wrote in message
news:Xn*******************************@140.99.99.1 30...
hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:up**************@TK2MSFTNGP09.phx.gbl:
* koorb <ko***@raidrs.co.uk> scripsit:
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)


"/" -> "\".


\ / doesn't really matter.

Nov 21 '05 #9

P: n/a

Use FormatDateTime("ur date parameters", DateFormat.GeneralDate)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.