Format date to YYYY-MM-DD

I think this is easy but as new programmer I can't find it out.

I want the user to fill in the date in a textbox and then I want to check the textbox that they write the date in format YYYY-MM-DD and if they didn't, I show a message. I have tried IsDate but it doesn't work as I want.

Thanks
CarrolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MennovdhCommented:
f IsDate(Text1.Text) Then
        If Text1.Text = Format(Text1.Text, "YYYY-MM-DD") Then
            'do something
        Else
            MsgBox "wrong format"
        End If
    Else
        MsgBox "Invalid Date"
    End If

Although I personally think it's silly to make users enter dates in a predefined format if you can easily convert it anyway.
0
dhamijapCommented:
Mennovdh:
You double checked and it is wrong. this will work


carrol:
try this
    If IsDate(Text1.Text) Then
        MsgBox Format(Text1.Text, "YYYY-MM-DD")
        'your code processing
    Else
       MsgBox "Invalid Date"
       'your code processing
   End If
0
JacamarCommented:
dim Date as String

if IsDate(Text1.text) = True then
  Date = DateValue(Text1.text)
  msgBox (Date,vbInformation)
Else
  msgBox("Please input a proper date")
End if

Try this.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JoengCommented:
Why don't you use date picker control and set the date format as YYYY-MM-DD. Rgds.
0
MennovdhCommented:
dhamijap:

As far as I understood, Carrol wanted to check if a user entered a date in the YYYY-MM-DD format. My code does exactly that. Yours doesn't.
0
CarrolAuthor Commented:
Thank's everybody for help
Joeng:
I don't know how and why should I

Mennovdh:
Yes, your comment is right, I want the user to check the date in that format so it's ok.  I have two textbox, one for "From date" and one "To date" that I want to check. Why doesn't this work;

     If (IsDate(Text1.Text)) Or (IsDate(Text2.Text)) Then
       If (Text1.Text= Format(Text1.Text, "YYYY-MM-DD"))   Or (Text2.Text = Format(Text2.Text, "YYYY-MM-DD")) Then
           'date is ok, do nothing
       Else
           MsgBox "Wrong format, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
           Exit Sub
       End If
     Else
        MsgBox "Invalid Date, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
        Exit Sub
     End If
0
MennovdhCommented:
replace your Or's by And's

or if you don't want it to do anything if the format is correct:

If (IsDate(Text1.Text)) And (IsDate(Text2.Text)) Then
      If (Text1.Text<> Format(Text1.Text, "YYYY-MM-DD"))   Or (Text2.Text <> Format(Text2.Text, "YYYY-MM-DD")) Then
          MsgBox "Wrong format, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
          Exit Sub
      End If
    Else
       MsgBox "Invalid Date, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
       Exit Sub
    End If
0
CarrolAuthor Commented:
In this case you dont need to input date in both textbox so I can't use And, more suggestion?
0
CarrolAuthor Commented:
In this case you dont need to input date in both textbox so I can't use And, more suggestion?
0
MennovdhCommented:
this is starting to get beyond the scope of the original question, but how about you write a function that checks the date; like this:

Private Sub Command1_Click()
   
    If (Len(Trim(Text1.Text)) = 0 Or IsValidDate(Text1.Text)) And (Len(Trim(Text2.Text)) = 0 Or IsValidDate(Text2.Text)) Then
        'Do whatever you want to do if the dates are valid
    End If
   
End Sub

Private Function IsValidDate(DateString As String) As Boolean

    IsValidDate = False

    If IsDate(DateString) Then
        If DateString = Format(DateString, "YYYY-MM-DD") Then
           IsValidDate = True
        Else
            MsgBox "Wrong format, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
        End If
    Else
        MsgBox "Invalid Date, Please input date like 'YYYY-MM-DD' ", vbInformation, "Find Date"
    End If

End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CarrolAuthor Commented:
Thank's for your help and time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.