Link to home
Start Free TrialLog in
Avatar of Carrol
Carrol

asked on

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
Avatar of Mennovdh
Mennovdh

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.
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
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.
Why don't you use date picker control and set the date format as YYYY-MM-DD. Rgds.
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.
Avatar of Carrol

ASKER

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
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
Avatar of Carrol

ASKER

In this case you dont need to input date in both textbox so I can't use And, more suggestion?
Avatar of Carrol

ASKER

In this case you dont need to input date in both textbox so I can't use And, more suggestion?
ASKER CERTIFIED SOLUTION
Avatar of Mennovdh
Mennovdh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Carrol

ASKER

Thank's for your help and time.