Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hye all!

Can anyone solve my problem?? I want to convert ID in varchar to Date, then back to string to save in varchar type.

Straight to the point, here is the example:
ID(varchar) - 891217

I want to convert the 89 part to 1989. so if any ID starting with 00, it will convert to 2000.(of course this needs the date to be in Date format, right?)

ID (891217) --> BirthDate = 17/12/1989

17/12/1989 --> I want to convert this back to string to save into database which is in varchar datatype.
Posted

Have a look at the TryParseExact method[^].

You can provide the exact format as one of the parameters of the function as describer here[^].
 
Share this answer
 
Comments
snamyna 19-Oct-11 21:59pm    
Thanks for your response.
Function for converting to string from date format

VB
'Convert Date to DateString
    Function DateYYYYMMDD(ByVal pDate As Date) As String

        Dim Date_str As String = Nothing
        'For Year Concatnation
        Date_str = pDate.Year()

        'For Month Concatnation
        If (pDate.Month().ToString().Length > 1) Then
            Date_str = Date_str & pDate.Month()
        Else
            Date_str = Date_str & "0" & pDate.Month()
        End If

        'For Day Concatnation
        If (pDate.Day().ToString().Length > 1) Then
            Date_str = Date_str & pDate.Day()
        Else
            Date_str = Date_str & "0" & pDate.Day()
        End If

        Return Date_str

    End Function


Function for converting from string to date
Function Date_convert(ByVal strDate As String) As Date
     DateTime dt = (DateTime)(TypeDescriptor.GetConverter(new   DateTime(1990,5,6)).ConvertFrom(strDate));
 End Function
 
Share this answer
 
Comments
snamyna 19-Oct-11 22:00pm    
Thanks for your response

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