Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Convert dates entered by user as strings to VB Date values.

Total Hit ( 3693)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This code formats a date string entered by the user, adding slashes and correct number of year digits and converts it to a proper date format.
You can use the following code to make your user interface more friendly. It will help you in correcting and preventing mistakes over what date is entered. Also the user will be able to immediately see what your program is going to accept and correct if necessary.

Just add the code given below to your form class to create a function to format the date.

Click here to copy the following block
Private Function StringToDate(ByRef DateString As String) As Boolean

'if the user did not type slashes, guess, based on the length of string
If InStr(DateString, "-")=0 And InStr(DateString, "/") = 0 InStr(DateString, "\") = 0 Then
If DateString.Length = 4 Then
  DateString = DateString.Substring(0,2) & "/" & DateString.Substring(2)
ElseIf (DateString.Length = 6 Or DateString.Length = 8) Then
 DateString = DateString.Substring(0,2) & "/" & DateString.Substring(2,2)_
 & "/" & DateString.Substring(4)
End If

End If
'If the above formatting works and u convert the string to date...
Try
 DateString = Format(Convert.ToDateTime(DateString), "MM/dd/yyyy")
 Return True

Catch e as Exception
 Return False
End Try

To use it in your forms, add the following code to the Leave event of your text box.

Click here to copy the following block
Dim s as String = TextBox1.Text
If StringToDate(s) Then TextBox1.Text = s

When u run it, enter some dates using 2 digit years and dashes instead of slashes and as you tab from it, the date will be reformatted nicely, providing confirmation to the user.


Submitted By : Saikiran Jetti  (Member Since : 8/22/2004 1:01:36 PM)

Job Description : Computer Genius
View all (2) submissions by this author  (Birth Date : 4/7/1980 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.