ARTICLE

String to System date conversion

Posted by Ivar Red Articles | Visual Basic 2010 January 25, 2011
In this article you will learn how to use String to System date conversion.
 
Reader Level:

During my programming I was facing a problem with String to date Conversion.  Most of the time I managed to convert the String date to my system default date format "DD/MM/YYYY".

But later on as I'm going to develop projects I'm repeatedly facing problems with this date conversion and searching in the net for a solution. But as this may be a small issue for others I didn't find any proper solution for that. But I tried to build a small function which will convert the user date string to system date. So here I'm posting this code to help my fellow developers.

The code will take date string in "DD/MM/YYYY" format and convert that to a system date.

Code:

Private Function Converttodate(ByVal filedate As String) As Date
       
Dim sysdateformat As String = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern
       
Dim dateseperator As String = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator

        Dim sysdatesplit() As String = sysdateformat.Split(dateseperator)
       
Dim filedatesplit() As String = filedate.Split("/")
       
Dim file_DD As String = filedatesplit(0)
       
Dim file_MM As String = filedatesplit(1)
       
Dim file_YY As String = filedatesplit(2)
       
Dim filedatenew As String = ""
       
filedatesplit = Nothing
       
ReDim filedatesplit(3)
       
For i As Int16 = 0 To sysdatesplit.Length - 1
           
If sysdatesplit(i).ToUpper.Contains("D") Then

                filedatesplit(i) = file_DD
           
ElseIf sysdatesplit(i).ToUpper.Contains("M") Then
               
filedatesplit(i) = file_MM
           
ElseIf sysdatesplit(i).ToUpper.Contains("Y") Then
               
If sysdatesplit(i).Length = 4 Then
                   
filedatesplit(i) = file_YY
               
ElseIf sysdatesplit(i).Length = 2 Then
                   
filedatesplit(i) = file_YY.Substring(2)
               
End If
           
End If
       
Next
       
filedatenew = filedatesplit(0) & dateseperator & filedatesplit(1) & dateseperator & filedatesplit(2)

        Return Date.Parse(filedatenew, Nothing)
   
End Function

I don't know whether there may be any functions that directly do this job in vb.net as I'm very new to this.

Please send me you valuable feedback on this.

Happy Programming…. :)

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor