Check valid dates and times for proper format vb6

isnoend2001
isnoend2001 used Ask the Experts™
on
I have a mshflexgrid that is editable by using a textbox and has 2 columns of dates/times
these dates/ times do calculations and need to be in the proper format
How can i determine if they are entered correctly ?
date time
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Dim MyDate As String

MyDate = "11/06/2015"

If IsDate(MyDate) Then
    MsgBox "Valid"
Else
    MsgBox "Not valid"
End If

Open in new window

Author

Commented:
Thanks Marty
will that work for times also ?
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Yes.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks

Author

Commented:
how could this be valid : "11/15/896" is it my vb or ?
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
That is surprising and while 896 is a year I would think that VB wouldn't consider it one since the usual minimum is 1900 (I think). In any case try this. The added IsDateEx function by default accepts only dates +/- 20 years from today's date. It could be changed to make it so the date has to be after today's date and the 20 years could be changed either by changing the default in the function or passing a smaller value as the second parameter to that function like IsDateEx(MyDate, 3) to ensure that a date can't be after 2018.

Private Sub YourSub()
Dim MyDate As String

MyDate = "11/15/896"

If IsDateEx(MyDate) Then
    MsgBox "Valid"
Else
    MsgBox "Not valid"
End If
End Sub

Private Function IsDateEx(strDate As String, Optional intSpan As Integer = 20) As Boolean

If IsDate(strDate) And Year(strDate) - intSpan <= Year(Now) And Year(strDate) + intSpan >= Year(Now) Then
    IsDateEx = True
Else
    IsDateEx = False
End If
End Function

Open in new window

Author

Commented:
thanks glad you added that
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I just realized that IsDateEx probably won't work for times.

Author

Commented:
I have not tested times for something out of the ordinary only, that it will be a time eg:
With gridMaster
if .textmatrix(.row, .col-1 = "Daily" then
it will have to a time
i will do some testing with times only thing i did so far was check if a space
was needed before the AMPM and it worked with a space and without

Author

Commented:
found a problem this should not be a valid time, but vb says it is
"13:30am" vb says it's valid
i don't think any number over 12 should be valid for the hour in AM?PM format

Author

Commented:
this is valid by vb "14:36am"
but this not
"10:61am"
it seems the minutes are correct, but not the hours
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please start a new question.

Author

Commented:
ok in just a few minutes

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial