Testing for <Incorrect> Dates

T

Tim Childs

I have somedates in this text/label format (yyyy-mm-dd) i.e. not numbers,

2007-02-01

2007-01-33

2007-02-01



I want to test for valid dates. The basic approach I was using was this:



Sub foo()

Dim sDateStr As String



sDateStr = "2007-01-33"

Debug.Print DateSerial(Left(sDateStr, 4), Mid(sDateStr, 6, 2),
Right(sDateStr, 2))

Debug.Print IsDate(DateSerial(Left(sDateStr, 4), Mid(sDateStr, 6, 2),
Right(sDateStr, 2)))



End Sub



But pseudo-dates such as

33rd January 2007 (see second entry)

get interpreted as 2nd February 2007 and *pass* the test.



What is the best way of testing for such pseudo-dates, please?



Thanks in advance



Tim
 
R

Rick Rothstein \(MVP - VB\)

I have somedates in this text/label format (yyyy-mm-dd) i.e. not numbers,
2007-02-01

2007-01-33

2007-02-01

I want to test for valid dates. The basic approach I was using was this:



Sub foo()

Dim sDateStr As String

sDateStr = "2007-01-33"

Debug.Print DateSerial(Left(sDateStr, 4), Mid(sDateStr, 6, 2),
Right(sDateStr, 2))

Debug.Print IsDate(DateSerial(Left(sDateStr, 4), Mid(sDateStr, 6, 2),
Right(sDateStr, 2)))

End Sub

But pseudo-dates such as

33rd January 2007 (see second entry)

get interpreted as 2nd February 2007 and *pass* the test.

What is the best way of testing for such pseudo-dates, please?

If IsDate(sDateStr) Then
' Date is OK
Else
' Date is bogus
End If

Rick
 
T

Tim Childs

Hi Rick

thanks - very straightforward. I just wish I had been able to puzzle it out
myself!

bw

Tim
 
G

Guest

? isdate("3rd January 2007")
False

is that what you meant?

I guess I misunderstood your intentions.
 

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