473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(inputArra y(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.Invalid CastException' occurred
in microsoft.visua lbasic.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 3472
koorb <ko***@raidrs.c o.uk> wrote in
news:sm******** *************** *********@4ax.c om:
The error I get is:
An unhandled exception of type 'System.Invalid CastException' occurred
in microsoft.visua lbasic.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********@rog ers.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********@rog ers.com>
wrote:
koorb <ko***@raidrs.c o.uk> wrote in
news:sm******* *************** **********@4ax. com:
The error I get is:
An unhandled exception of type 'System.Invalid CastException' occurred
in microsoft.visua lbasic.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.c o.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******** ******@TK2MSFTN GP09.phx.gbl:
* koorb <ko***@raidrs.c o.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**********@y ahoo.com> scripsit:
* koorb <ko***@raidrs.c o.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**********@p lanet.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.LastInde xOf 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.LastInde xOf 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.LastInde xOf to include VB.InStrRev & String.IndexOf
to include VB.InStr.

Just a thought
Jay

"Anon-E-Moose" <an**********@y ahoo.com> wrote in message
news:Xn******** *************** ********@140.99 .99.130...
hi************* **@gmx.at (Herfried K. Wagner [MVP]) wrote in
news:up******** ******@TK2MSFTN GP09.phx.gbl:
* koorb <ko***@raidrs.c o.uk> scripsit:
FileOpen(1, "F:/DestinationTime .txt", OpenMode.Input)


"/" -> "\".


\ / doesn't really matter.

Nov 21 '05 #9

Use FormatDateTime( "ur date parameters", DateFormat.Gene ralDate)
*** 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
10517
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 using C# eg. - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from the database. - I do not care about the time portion, I only want "1/1/2005" for display.
2
12727
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 necessary for variuos purposes. There is something wrong with the following code. I get the following error when I enter in the above date string:
38
807
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 FormatString, how can you convert the String back to a DateTime? DateTime.Parse(...) doesn't use the FormatString. Now admitedly, if the format string is just "MM", it can't be done. But if the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it...
33
4698
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
15
50273
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
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.