date formats

S

Sam

Hi,
In vb.net, I want to let the user enter a date format in a textbox.
e.g : mm/dd/yyyy
dd/mm/yyyy
dd-mm-yyyy

Then, how can I check this is a valid date format and how can I build
an example so that it is displayed in another textbox ?

12/31/2005

Regards
 
K

Ken Tucker [MVP]

Hi,

Take a look at the datetime parse and parseexact methods

http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatetimeclassparsetopic.asp

http://msdn.microsoft.com/library/d...l/frlrfsystemdatetimeclassparseexacttopic.asp

Ken
-------------------------
Hi,
In vb.net, I want to let the user enter a date format in a textbox.
e.g : mm/dd/yyyy
dd/mm/yyyy
dd-mm-yyyy

Then, how can I check this is a valid date format and how can I build
an example so that it is displayed in another textbox ?

12/31/2005

Regards
 
S

Sam

Thanks but it's not exactely what I want.
Again I want to check that the string entered by the user (e.g
DD/MM/yyyy) is a valid date format and then prove it by displaying an
example (14/12/2005)
 
K

Ken Tucker [MVP]

Hi,

You could use a regex to check that it is a valid date format or
use a try catch block


Regex

Dim rValid As New System.Text.RegularExpressions.Regex("(0[1-9]|1[012])[-
/.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d")

If rValid.IsMatch(TextBox1.Text) Then

Dim dt As DateTime

dt = DateTime.Parse(TextBox1.Text)

TextBox2.Text = dt.ToLongDateString

Else

MessageBox.Show("Invalid date format")

End If




Try catch block

Try

Dim dt As DateTime

dt = DateTime.Parse(TextBox1.Text)

TextBox2.Text = dt.ToLongDateString

Catch

MessageBox.Show("Invalid date format")

End Try



Ken

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


Thanks but it's not exactely what I want.
Again I want to check that the string entered by the user (e.g
DD/MM/yyyy) is a valid date format and then prove it by displaying an
example (14/12/2005)
 
C

Cor Ligthert

Ken
You could use a regex to check that it is a valid date format
or
use a try catch block
In my opinion that is impossible the standard IsDate is very fine for that.

With using a regex you would allow in the first two 0-31 or test that
31-12 is allowed however
12-31 not

However the big problem is that you cannot do anything with that date. How
do you know what is
1-12-2005 without culture/language info info.

For you it is january 12 for me it is 1 december.

Cor
 
H

Herfried K. Wagner [MVP]

Sam said:
Again I want to check that the string entered by the user (e.g
DD/MM/yyyy) is a valid date format and then prove it by displaying an
example (14/12/2005)

I doubt that there is a way to check if a string is a valid date format,
because date format strings can contain arbitrary characters.
 
S

Sam

I haven't tried IsDate yet, but I have tried Ken's idea with the try
catch block :

Try
Dim dt As DateTime
dt = DateTime.Parse("dddd - d - MMMM")
TextBox1.Text = dt.ToLongDateString
Catch
MessageBox.Show("Invalid date format")
End Try

However although dddd - d - MMMM is a valid format, it keeps raising an
exception :
The string was not recognized as a valid DateTime. There is a unknown
word starting at index 0.

Why that ?
Thx anyway for the replies guys
 
S

Sam

Cor,
I've tried IsDate :

?isdate("dd mmm yyyy")
False
?isdate("mm/dd/yyyy")
False
?isdate("dd/mm/yyyy")
False
?isdate("dd-mm-yyyy")
False

Can you explain that to me ? Those should be valid date format !
 
S

Sam

damn it.. I posted too quickly. Obviously IsDate expects a date, but
then it seems hard to convert the string format entered by the user to
a date... I'll try
 
S

Sam

I think I've got it :) Using these function :
Note that MyDate.ToString will get what the user entered in my textbox.

Dim MyDate As New DateTime(2005, 12, 31)
Dim MyString As String = MyDate.ToString("yyyy-MM-dd")

Dim res As Boolean = IsDate(MyString)

Thx
 
C

Cor Ligthert

Sam,

As I wrote in this thread, the way dates are uses is depending on the
culture/language.

In Quibec it is the same as with us, however in Edmunton it is as in the
USA.

However I saw your latter message, therefore I assume you saw that already.

Cor
 
S

Sam

Cor,
What if I want to check for any kind of regional settings ?

For example "MM/dd/yyyy" is not valid on my machine but "dd/MM/yyyy" is
valid, and I want both of them to be valid, how ?

Thx
 
H

Herfried K. Wagner [MVP]

Sam said:
I've tried IsDate :

?isdate("dd mmm yyyy")
False
?isdate("mm/dd/yyyy")
False
?isdate("dd/mm/yyyy")
False
?isdate("dd-mm-yyyy")
False

Can you explain that to me ? Those should be valid date format !

'IsDate' is not intended for checking the validity of a date format string.
It's used to check the validity of a string representing a certain date.
 
H

Herfried K. Wagner [MVP]

Sam said:
What if I want to check for any kind of regional settings ?

For example "MM/dd/yyyy" is not valid on my machine but "dd/MM/yyyy" is
valid, and I want both of them to be valid, how ?

AFAIS every string is a valid date format, but an infinite number of date
strings won't contain the date in a readable form.
 
C

Cor Ligthert

Sam,

Microsoft Net works only on computers which has there culture and language
setting, therefore should the right date be checked right on every computer.

In Paris using IsDate would a string 12/31/2005 be wrong and in New York
31/12/2005 if the user has not played with his culture settings.

(This is by the way not for webpages, in that you should have to find out
what culture the user is using).

I hope this helps,

Cor
 
S

Sam

Cor,
I understand this. My question was : is there a way to override this
behavious, that is prevent the application to check for regional
settings, OR check for every possible regional settings ?

Thx
Sam
 

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