Invalid cast from string to Date.

S

Simon Says

Dear all,

I have the following:

dim tmpString as String = "11/21/2004 07:55:33"
dim tmpDate as Date = CDate(tmpString)

But I keep getting <cast from string "11/21/2004 07:55:33" to type 'Date' is
not valid.> Is there anything that I did wrong?

Thanks.

--Simon
 
K

Ken Tucker [MVP]

Hi,

Dim tmpString As String = "11/21/2004 07:55:33"

Dim tmpDate As Date = DateTime.Parse(tmpString)



Ken

---------------

Dear all,

I have the following:

dim tmpString as String = "11/21/2004 07:55:33"
dim tmpDate as Date = CDate(tmpString)

But I keep getting <cast from string "11/21/2004 07:55:33" to type 'Date' is
not valid.> Is there anything that I did wrong?

Thanks.

--Simon
 
C

Cor Ligthert

Simon,

For non USA timezone using visitors to this newsgroup.

\\\
Dim mydate As String = _
New Date(2004, 11, 21, 7, 55, 33).toString
tmpDate = CDate(mydate)
///

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

Simon Says said:
I have the following:

dim tmpString as String = "11/21/2004 07:55:33"
dim tmpDate as Date = CDate(tmpString)

But I keep getting <cast from string "11/21/2004 07:55:33" to type 'Date'
is
not valid.> Is there anything that I did wrong?

Use 'DateTime.Parse' or 'DateTime.ParseExact' and specify an appropriate
'CultureInfo' object. If you don't know the culture of the date format, use
'CultureInfo.InvariantCulture' + an explicit date/time format string in
'DateTime.ParseExact'.
 
J

JD

The OP wanted to know how to go from a string to a date. You show how to go
from a date to a string to a date. If he had the date in the first place
would he need to ask his question? Does that make sense?
 
C

Cor Ligthert

JD,

The OP wanted to know how to go from a string to a date. You show how to
go
from a date to a string to a date. If he had the date in the first place
would he need to ask his question? Does that make sense?
Do you mean he wants to hard code in this way a date in his program. I
assume you don't do it that way and I assume the OP does not do that either.

In my opinion his datestring can come from a document or a textbox or
whatever and is he just giving it as an example.

Therefore I show a sample that works in every culture.

Cor
 
J

JD

Cor Ligthert said:
JD,


Do you mean he wants to hard code in this way a date in his program. I
assume you don't do it that way and I assume the OP does not do that either.

In my opinion his datestring can come from a document or a textbox or
whatever and is he just giving it as an example.

Exactly. The OP starts off with a *string* that can come from anywhere. So
how does your example help him when in your example you start off with a
*date* not a *string*?
 
C

Cor Ligthert

The OP wanted to know how to go from a string to a date. You show how
Exactly. The OP starts off with a *string* that can come from anywhere. So
how does your example help him when in your example you start off with a
*date* not a *string*?
JD

Why do you think that you understand that in the sample that string is first
made and the OP does not?

Cor
 
J

JD

Cor Ligthert said:
JD

Why do you think that you understand that in the sample that string is first
made and the OP does not?

Because the OP is asking how to convert a *string* to *date*. Why do you
think he has a date first?
 
C

Cor Ligthert

JD said:
Because the OP is asking how to convert a *string* to *date*. Why do you
think he has a date first?
Can you give the exact answer for that.
By instance for the string "JD"

Cor
 
C

Cor Ligthert

I have absolutely no clue what you are talking about...
Strange you are all the time answering in line, and now you do not know when
I am talking about when I ask this accoording to your messageShow me how you convert the "string" "JD" to a "date".

Cor
 
J

JD

Cor Ligthert said:
Strange you are all the time answering in line, and now you do not know when
I am talking about when I ask this accoording to your message
Show me how you convert the "string" "JD" to a "date".

Why?
 
J

JD

Cor Ligthert said:
Because you are telling that, that is the question and my answer is not
right.

Or can you only tell other people what is wrong?

Cor

No I'm not. I think you are confused.
 
C

Cor Ligthert

JD said:
No I'm not. I think you are confused.
You mean you did not write that bellow, are there more JD's or is it you who
is confused?

Exactly. The OP starts off with a *string* that can come from anywhere. So
how does your example help him when in your example you start off with a
*date* not a *string*?

Show me starting with a string with the value "JD" how you convert that to a
datetime.

Cor
 
J

JD

Cor Ligthert said:
You mean you did not write that bellow, are there more JD's or is it you who
is confused?

Exactly. The OP starts off with a *string* that can come from anywhere. So
how does your example help him when in your example you start off with a
*date* not a *string*?

Show me starting with a string with the value "JD" how you convert that to a
datetime.

Cor

Thats called *validation*, the OP was not trying to *validate* the string,
he was trying to *convert* the date string.
 
C

Cor Ligthert

JD,

Maybe you still don't understand it, however in my culture as in a lot of
others is the string he showed the same invalid as "JD"
Thats called *validation*, the OP was not trying to *validate* the string,
he was trying to *convert* the date string.

And because there are not only visitors from the US here, I showed it in a
way that it would work in all cultures. This newsgroup is used for reference
as well.

Maybe you find it rude, however I don't add anything more to this thread.

Cor
 
J

JD

Cor Ligthert said:
JD,

Maybe you still don't understand it, however in my culture as in a lot of
others is the string he showed the same invalid as "JD"


And because there are not only visitors from the US here, I showed it in a
way that it would work in all cultures. This newsgroup is used for reference
as well.

I perfectly understand the culture differences but you are clearly still
confused. The question at hand was "how to convert a string, in a specific
culture format, to a date". Herfried's reply was correct. Your example
showed "how to convert a date to a string, back to a date again", which
really doesn't make sense when you can recieve date strings from around the
world through web services, files, etc., all with different formats.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
And because there are not only visitors from the US here, I showed it in a
way that it would work in all cultures. This newsgroup is used for
reference as well.

How would it work if the string is read from a file?

Your sample doesn't show how to convert a date string in a certain
(uninfluencable) format to a 'Date'.
 

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