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?

Dani AI

Generated

Good callouts from @Unhnd_Exception and @Mitja Bonca: free-form numeric dates are ambiguous and brittle. If this is WinForms, a DateTimePicker (or a MaskedTextBox as noted) gives you consistent input. If this is ASP.NET Web Forms, prefer a calendar/date picker widget; only fall back to parsing raw text when you must. Also decide the one true format you will accept from users (e.g., MMddyy) and reject everything else. That removes guesswork like 040912 meaning April 9 vs. 4 September.

If you do need to accept 6 digits like 040912 and display 04/09/12, validate and parse on the server (or in WinForms, in Validating) using DateTime.TryParseExact. This avoids exceptions and rejects impossible dates.

Imports System.Globalization
Imports System.Text.RegularExpressions

' For ASP.NET: call in server-side event (e.g., Button_Click)
' For WinForms: call from TextBox.Validating
Sub NormalizeSixDigitDate()
    Dim raw As String = txtDate.Text.Trim()

    If Not Regex.IsMatch(raw, "^\d{6}$") Then
        ShowError("Enter exactly 6 digits in MMddyy format.")
        Return
    End If

    Dim parsed As DateTime
    ' Accept only MMddyy to remove ambiguity. Add other patterns if you truly must.
    If DateTime.TryParseExact(raw, "MMddyy", CultureInfo.InvariantCulture, DateTimeStyles.None, parsed) Then
        txtDate.Text = parsed.ToString("MM/dd/yy", CultureInfo.InvariantCulture)
    Else
        ShowError("That is not a valid calendar date.")
    End If
End Sub

Notes:

  • Prefer storing full dates (DateTime/Date) and formatting only for display. If you ever persist text, use a 4-digit year (yyyy) to avoid two-digit year rollover surprises.
  • For WinForms, a MaskedTextBox with a date mask still needs validation; the mask only controls shape, not correctness.
  • For Web Forms, pair client checks with server validation to handle copy/paste and disabled scripts.
  • If your audience is international, consider ddMMyy or (better) force a picker and display the date using the user’s culture while storing a neutral ISO-friendly format.

Recommended Answers

All 4 Replies

Member Avatar for Member #857553

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.