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…. :)