Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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?
VB
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 :)
Posted
Updated 10-Feb-12 15:09pm
v2
Comments
Christian Graus 10-Feb-12 17:22pm    
'better than this code' is sort of what this post is about, but not really a good subject line. 'Converting strings to datetime' perhaps ?

Something like this might work for you;

VB
Private Function ConvertToDate(ByVal d As String) As DateTime
    Dim result As DateTime
    If DateTime.TryParse(d, result) Then
        Return result.Date
    Else
        Throw New ArgumentException("not a valid date", "d")
    End If
End Function


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Espen Harlinn 11-Feb-12 10:39am    
5'ed!
C#
string strinput = "2011-Feb-09 04:30:30"; //yyyy-MMM-dd hh:mm:ss

           DateTime dt = Convert.ToDateTime(strinput, System.Globalization.CultureInfo.InvariantCulture);
           Response.Write(dt.ToString("dd-MM-yyyy"));


Does this help?
 
Share this answer
 
Comments
Espen Harlinn 11-Feb-12 10:39am    
5'ed!
That's Aragon 13-Feb-12 0:34am    
Thanks
Actually it returns a DateTime object initialized with year, month and day of parsed string.

I guess you may obtain the same result with
VB
Private Function ConvertToDate(ByVal d As String) As DateTime
  Dim dt As DateTime = DateTime.Parse(d)
  Return New DateTime(dt.Year, dt.Month, dt.Day)
End Function
 
Share this answer
 
Comments
Christian Graus 10-Feb-12 17:21pm    
Um... why do you call DateTime.Parse instead of TryParse, and why do you then create a new DateTime out of the one you already created ?
CPallini 10-Feb-12 17:58pm    
Well, TryParse is actually better, while a new DateTime is a simple way to reset time (hours, minutes, seconds) information.
Christian Graus 10-Feb-12 20:57pm    
OK, so I was right on one front, but you're using a new one to remove the time ?
CPallini 11-Feb-12 11:18am    
Well, assuming the input is correct (as precondition), you don't need TryParse. However yes, you are right.
Yes, I use a new instance for removing time (I am aware it could be inefficient).
Espen Harlinn 11-Feb-12 10:39am    
5'ed!
First of all, your code checks for Fep and not Feb. Is this a localisation thing ?

Second, this is a continuation of someting I answered yesterday and the answer is the same. You will need to use substrings to pull out the three numbers, convert them and build a date time from there. Also, don't post an 'answer' to ask a question. An answer and a question are opposite things. Edit your post.
 
Share this answer
 
Comments
Malath Razooq 11-Feb-12 4:18am    
Thanks for advice,
About Fep.. Actually I spent 2 hours until I've found that the inserted data is contains Fep not Feb :P

I can pull it out as the old code above, but I'm asking if there is built-in function to parse "yyyymmdd" to Date
Christian Graus 11-Feb-12 9:07am    
Actually if you read this : http://technet.microsoft.com/en-us/query/9h21f14e if you can write your own IFormatProvider and any other arguments used to specify the format, then the answer is yes, but you will probably end up writing the code I've been telling you that you need to write. The code I'm talking about takes about 10 minutes to write, so I don't see why you've spent days asking this.
Malath Razooq 11-Feb-12 10:44am    
Well thanks a lot, I've been learning VB.NET by myself thats why I'm asking..

I've created my first project, and trying to enhance the written code..

Thanks for your time and help :).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900