Monday, December 14, 2009

How to convert the string to DateTime ?

Common Function:

Public Function ConvertoDate(ByVal dateString As String,
 ByRef result As DateTime) As DateTime   
Try             
     Dim supportedFormats() As String = New String() 
{"dd/MM/yyyy", "MM/dd/yyyy", "MM/dd/yy", 
"ddMMMyyyy", "dMMMyyyy"}             
     result = DateTime.ParseExact(dateString, 
supportedFormats, System.Globalization.CultureInfo.
CurrentCulture, System.Globalization.DateTimeStyles.None)  
     Return result    
Catch ex As Exception   
    Return Nothing      
End Try    
End Function 
We should enter the Date below format. Then only it 
will convert into DateTime.

Dim supportedFormats() As String = New String() {"dd/MM/yyyy", 
"MM/dd/yyyy", "MM/dd/yy", "ddMMMyyyy", "dMMMyyyy"}


Calling the function

ConvertoDate(frmDate, TfrmDate)
frmDate - String
TfrmDate - DateTime object

No comments:

Post a Comment