?
Solved

Format date to YYYY-MM-DD

Posted on 2003-02-24
11
Medium Priority
?
520 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:Carrol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 2

Expert Comment

by:Mennovdh
ID: 8008586
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
 

Expert Comment

by:dhamijap
ID: 8009308
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
 
LVL 2

Expert Comment

by:Jacamar
ID: 8009803
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Joeng
ID: 8014350
Why don't you use date picker control and set the date format as YYYY-MM-DD. Rgds.
0
 
LVL 2

Expert Comment

by:Mennovdh
ID: 8014763
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
 

Author Comment

by:Carrol
ID: 8015510
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
 
LVL 2

Expert Comment

by:Mennovdh
ID: 8015586
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
 

Author Comment

by:Carrol
ID: 8015662
In this case you dont need to input date in both textbox so I can't use And, more suggestion?
0
 

Author Comment

by:Carrol
ID: 8015806
In this case you dont need to input date in both textbox so I can't use And, more suggestion?
0
 
LVL 2

Accepted Solution

by:
Mennovdh earned 280 total points
ID: 8016094
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
 

Author Comment

by:Carrol
ID: 8023371
Thank's for your help and time.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

801 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