CDate problem

  • Thread starter Thread starter David Schwartz
  • Start date Start date
D

David Schwartz

I have a few users who are getting the error "Cast from string '1/1/2004' to
type 'Date' is not valid".

The code which causes the error is the following:

Dim LoginString As String = "1/1/2004"
LoginDate = CDate(LoginString)

Any idea what might be causing this error?

Thanks!
 
Is 1/1/2004 the actual date being used? I'm just wondering if you are
somehow getting that date from somewhere else, and it is invalid for one
reason or another. You may want to wrap that code with an IsDate() function
check.
 
David, I just copy and pasted that code (I had to add a declaration for
LoginData As Date) but it works fine. I tried CType and Date.Parse and all
worked.

Just for giggles I turned Off option strict and declared LoginDate as a
String using the same code as below and even then, it converted fine.

If that's the exact code snippet that is perplexing indeed.
 
Hi David,

Check the region settings on the problem machines - they probably have the
short date set in some unusual manner.

HTH,

Bernie Yaeger
 
* "David Schwartz said:
I have a few users who are getting the error "Cast from string '1/1/2004' to
type 'Date' is not valid".

The code which causes the error is the following:

Dim LoginString As String = "1/1/2004"
LoginDate = CDate(LoginString)

Any idea what might be causing this error?

Which system language and date separator do these machines have
selected?
 
Hi David,

1/1/2004, 2/2/2004, 3/3 till 12/12 goes always right
1/1/2004 til 12/12/2004 will never throw an error (values beneath the 13)
for the rest it is important what regional setting is on the computer.

I hope this helps,

Cor
 
Make sure you are useing american dates on the systems... if that is the
format you are useing... mm/dd/yyy if its ansi or any other format it would
cause this problem... it's all about the systems regional settings..
 
How can I programmatically get that information? (I don't trust the users
to give it to me properly!)

Thanks.
 
Hi David,

Have a look at

System.Globalization.CultureInfo

Know that to set this by hand there are differences in NTx and W9x
computers.

(W9x is using the keyboard settings for language).

I hope this helps?

Cor
 
* "David Schwartz said:
How can I programmatically get that information? (I don't trust the users
to give it to me properly!)

What information? Have a look at the 'System.Globalization' namespace,
for example 'System.Globalization.CultureInfo.CurrentUICulture'.
 
Dave,
Do you have a windows or web form , retreiving data from
the users. You might want to specify on the form what format you want the
users to eneter date in.
You can also parse the string to make sure it is a valid date or Use Isdate
functionality as someone mentioned.

Thanks,
Sarika
--------------------
 
I'm actually reading a string from the registry and then converting it to a
date. This fails for some users.
 
Back
Top