By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

date formats

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Hi,

Take a look at the datetime parse and parseexact methods

http://msdn.microsoft.com/library/de...parsetopic.asp

http://msdn.microsoft.com/library/de...exacttopic.asp

Ken
-------------------------
"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Nov 21 '05 #2

P: n/a
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)

Nov 21 '05 #3

P: n/a
Sam,

Normally should the IsDate function be enough

Cor
Nov 21 '05 #4

P: n/a
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

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

"Sam" <sa**************@voila.fr> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

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)
Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
"Sam" <sa**************@voila.fr> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

P: n/a
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

Nov 21 '05 #8

P: n/a
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 !

Nov 21 '05 #9

P: n/a
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

Nov 21 '05 #10

P: n/a
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

Nov 21 '05 #11

P: n/a
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
Nov 21 '05 #12

P: n/a
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

Nov 21 '05 #13

P: n/a
"Sam" <sa**************@voila.fr> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #14

P: n/a
"Sam" <sa**************@voila.fr> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #15

P: n/a
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

Nov 21 '05 #16

P: n/a
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

Nov 21 '05 #17

P: n/a
Sam,

This should go automaticly. (Not for a webpage)

I hope this helps,

Cor
Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.