hello everyone..
This function to convert String to Date, the input's string format is "yyyy-MMM-dd hh:mm:ss" and the return format dd-mm-yyyy
It is working perfectly..
My question is: is there any improvements for this code?
Private Function ConvertToDate(ByVal d As String) As DateTime
d = d.Replace(" 00:00:00", "")
d = d.Replace("/", "")
d = d.Replace("\", "")
d = d.Replace("-", "")
If (((d.ToLower.Contains("a") Or d.ToLower.Contains("e")) Or d.ToLower.Contains("u")) Or d.ToLower.Contains("o")) Then
If d.Contains("Jan") Then
d = d.Replace("Jan", "01")
End If
If d.Contains("Fep") Then
d = d.Replace("Fep", "02")
End If
If d.Contains("Mar") Then
d = d.Replace("Mar", "03")
End If
If d.Contains("Apr") Then
d = d.Replace("Apr", "04")
End If
If d.Contains("May") Then
d = d.Replace("May", "05")
End If
If d.Contains("Jun") Then
d = d.Replace("Jun", "06")
End If
If d.Contains("Jul") Then
d = d.Replace("Jul", "07")
End If
If d.Contains("Aug") Then
d = d.Replace("Aug", "08")
End If
If d.Contains("Sep") Then
d = d.Replace("Sep", "09")
End If
If d.Contains("Oct") Then
d = d.Replace("Oct", "10")
End If
If d.Contains("Nov") Then
d = d.Replace("Nov", "11")
End If
If d.Contains("Dec") Then
d = d.Replace("Dec", "12")
End If
d = d.Insert(d.Length, (Conversions.ToString(d.Chars(2)) & Conversions.ToString(d.Chars(3))))
d = d.Insert(d.Length, (Conversions.ToString(d.Chars(0)) & Conversions.ToString(d.Chars(1))))
d = d.Remove(0, 4)
End If
If (d = "") Then
Return New DateTime(1, 1, 1)
End If
Dim str3 As String = d.Remove(4)
Dim str2 As String = d.Remove(0, 4).Remove(2)
Dim str As String = d.Remove(0, 6)
Return New DateTime(Conversions.ToInteger(str3), Conversions.ToInteger(str2), Conversions.ToInteger(str))
End Function
Moved from Answer
Well, all of your codes is better and gave the same result..
but what if the input was "yyyymmdd", the previous function can do that, how to convert it with your codes?
Thank you all :)