Solved check if string is a date

Posted on 2009-07-12
Last Modified: 2012-05-07

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

Question by:MichMat
LVL 55

Accepted Solution

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 ....

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")
            MsgBox("Is NOT A Date")
        End If

Open in new window

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")
            MsgBox("Is NOT A Date")
        End If

Open in new window

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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.

Author Closing Comment

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.
LVL 39

Expert Comment

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 ;-)
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Receiving a string from a WebService Push 21 58
HTML - Color not displaying correctly in EMAIL. 6 49
Help with consolidating excel files using 2 29
Visual Studios 1 76
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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