Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

format datetime to use year with two digits only

i have a datetime variable that I need to format to use date with year of two last digit only.
how can i modify my current code to make this possible?

User generated image

thank you.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

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 metropia

ASKER

what happens when the date is a string entered by the user in a form text box?
how can the format be applied?

my textbox is: Me.txtDateMfg.Text
Applied when? When the data is posted to the server, or as the user is typing?
when the data is posted to the server?
SOLUTION
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
one of the events on the for, is from a preview button, after date is entered and button gets clicked, this function happens:

 Protected Sub cvDateMfg_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvDateMfg.ServerValidate
        If Date_IsValid(Me.txtDateMfg.Text) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Open in new window


Is it possible to format the date right here, or before?
i got that one wrong, the first code that gets executed is:

   Protected Sub cvDateMfg_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvDateMfg.ServerValidate
        If Date_IsValid(Me.txtDateMfg.Text) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Open in new window


Then this code:

 Protected Sub cvDateMfg_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvDateMfg.ServerValidate
        If Date_IsValid(Me.txtDateMfg.Text) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Open in new window



where would it be the right spot to specify the format? Thank you for your help.

I am sorry for the many posts, I am getting frustrated a bit.
this is how I ended up doing it:

    Protected Sub cvDateMfg_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvDateMfg.ServerValidate

        Dim dtDateMfg As DateTime = Nothing

        If DateTime.TryParse(Me.txtDateMfg.Text, dtDateMfg) Then
            Me.txtDateMfg.Text = dtDateMfg.ToString("MM/dd/yy")
        End If

        If Date_IsValid(Me.txtDateMfg.Text) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

Open in new window


Can you please let me know if you see anything wrong, or code that is not needed?

Thank you.
SOLUTION
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
SOLUTION
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