I have a textbox in my form where the individual will enter the date (not necessarily todays date). How can I set a format so if the individual enters 040912, it will change to 04/09/12?

Member Avatar for Unhnd_Exception

Probably a waste of time. You can format the input any way you want but you will need to check for all kinds of different input. for example they could enter the month first, no leading zeros , 4 digit year. You would need to check for all that. month and day swamped would be impossible. A DateTimePicker would work well.

Add a datetimepicker to the form

Set the following properties
Format = Custom
CustomFormat = "MM / dd / yy"
ShowUpDown = True

With that the format will always be the way you want it.

I completely agree with Unhnd. This is a waste of time. What if user inserts some other numbers, like 123456?
This will never work well.
You have to force user to SELECT a date, so choose between datetimepicker, or monthcalendar. I know I would defenatelly - bad experiances from before - belive me.

I would go with u both Mitja and Unhnd but what if we give the code in specific format

like if we tell the user to enter the date suppose yyddmm format and on validating event check and then convert the date....if not possible print error msg and if correct show the format...

like the one below....correct me if I m wrong....i know its waste of time for manual entry since user can enter any garbage value....

this code I have written in textbox validating event.

 Try

        Dim strDate As String = TextBox1.Text
        Dim d As Date = New Date(2000 + strDate.Substring(0, 2), strDate.Substring(2, 2), strDate.Substring(4, 2))
        Label6.Text = d.ToString("yyyy/MM/dd") 'Label6 will display the date

 Catch ex As Exception
        MsgBox(ex.Message)
 End Try

the datetimepicker is the best option to work upon

If you still want textbox then better way to go with maskedTextBox and setmask to shortdate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.