473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String to DateTime

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
9 3429
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
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
* 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
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
Herfried,

"/" -> "\".

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

Cor
Nov 21 '05 #6
* "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
* "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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.