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?

datetime assignment screen shot

thank you.
metropiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
A DateTime holds a date. Period. Formatting a date/time value is something you do when converting a DateTime object into its string representation. You can use the "yy" format modifier to accomplish this.

e.g.

Dim dtCodeDate As DateTime = ' some date
Dim strCodeDate As String = dtCodeDate.ToString("MM/dd/yy")  ' Case is important in this format string. "mm" is minutes, not months

Open in new window


A good reference for format modifiers is:  http://blog.stevex.net/string-formatting-in-csharp/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metropiaAuthor Commented:
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
0
käµfm³d 👽Commented:
Applied when? When the data is posted to the server, or as the user is typing?
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

metropiaAuthor Commented:
when the data is posted to the server?
0
käµfm³d 👽Commented:
OK, so you need to confirm that the user entered in a valid date. You can do this with the TryParse method:

Dim dtCodeDate As DateTime = ' some date

If DateTime.TryParse(Me.txtDateMfg.Text, dtCodeDate) Then
    ' date is valid
End If

Open in new window


Once you have a valid date, then you can proceed to format a string representation for it:

Dim dtCodeDate As DateTime = ' some date

If DateTime.TryParse(Me.txtDateMfg.Text, dtCodeDate) Then
    Dim strCodeDate As String = dtCodeDate.ToString("MM/dd/yy")  ' Case is important in this format string. "mm" is minutes, not months

    Me.txtDateMfg.Text = strCodeDate
End If

Open in new window

0
metropiaAuthor Commented:
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?
0
metropiaAuthor Commented:
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.
0
metropiaAuthor Commented:
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.
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. please try this example

DateTime expirationDate = new DateTime(2008, 1, 31); // random date
string lastTwoDigitsOfYear = expirationDate.ToString("yy");
0
Mel-Leo RosalCommented:
        Dim dtCodeDate As Date = DateValue(Now)
        MsgBox(dtCodeDate)
        MsgBox(Format(dtCodeDate, "MM/dd/yy"))

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.