Solved

VB.net check if string is a date

Posted on 2009-07-12
7
922 Views
Last Modified: 2012-05-07
Hi

I would like to check if a string is actually a date. However the issue is that I have to check that the string is actually a date in a spacific format. If the string isnt in a date certain format then I dont want to be recognised as a date.

The format of the date that Im looking to recognise is DDMMMYY
and example of this is 14Jun08

How would I do that ?

If Datestring = 14Jun08 the is date = true
If date string = 14Jun then date = NOT TRUE

I know its a wiered one but IsDate recognises the 14Jun as a valid date

Michal
0
Comment
Question by:MichMat
7 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
ID: 24836367
you need TryParseExact method:

Dim date as DateTime
If DateTime.TryParseExact(someString, "ddMMMyy", DateTimeStyles.None, date) Then ....

Reference: http://msdn.microsoft.com/en-us/library/ms131044.aspx
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 100 total points
ID: 24836400
Just don't forget the format provider (culture info).  Jaime's solution!
Dim s As String = "14Jun"

        

        Dim d As DateTime

        Dim enUS As New CultureInfo("en-US")

        If DateTime.TryParseExact(s, "ddMMMyy", enUS, DateTimeStyles.None, d) Then

            MsgBox("Is A Date")

        Else

            MsgBox("Is NOT A Date")

        End If

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24836403
Very nice, Jaime.  I was going to do this the harder way.
Dim s As String = "14Jun"

        'Imports System.Text.RegularExpressions

        If Regex.IsMatch(s, "\d{2}[A-Za-z]{3}\d{2}") And IsDate(s) Then

            MsgBox("Is A Date")

        Else

            MsgBox("Is NOT A Date")

        End If

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24836429
>Very nice, Jaime.  I was going to do this the harder way. '
How you will check if month abreviatures are valid? You will need a huge regex for that
TryParseExact is designed to fulfill author's requirement.
0
 

Author Closing Comment

by:MichMat
ID: 31602656
Being a novice I couldnt get Jamies to work intill the correction from mwvista1, I hope that its ok to then award the points to both as each had a part in the sucessfull solution . Thank you I would have never looked at that.
0
 
LVL 39

Expert Comment

by:abel
ID: 24836447
Just a few dots on the i if I may:

1. IsDate("35Jun08") will yield true. If you use IsDate and you want to know it is a valid date with this type of date, use the following:

If IsDate(s) AndAlso DateTime.Parse(s).Day = Val(s) Then ...

2. Reversing the logic and using AndAlso would be more efficient (IsDate is likely faster then IsMatch). But that's just a minor treat of course and hardly relevant.

3. The suggestion from jaime is missing the third parameter:

DateTime.TryParseExact(someString, "ddMMMyy", CultureInfo.InvariantCulture, DateTimeStyles.None, date)

Anyway, just my 2p. I know that you guys have given excellent suggestions already ;-)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24836535
Sorry for the missing parameter. Sometimes we don't have a machine with VS installed to fully check it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now