使用vb将String转换为Date并转换回String [英] Convert String to Date to String back using vb

查看:575
本文介绍了使用vb将String转换为Date并转换回String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

谁能解决我的问题?我想将varchar中的ID转换为Date,然后返回到字符串以保存为varchar类型.

直截了当,这里是示例:
ID(varchar)-891217

我想将89部分转换为1989.因此,如果有任何以00开头的ID,它将转换为2000.(当然,此日期需要为Date格式,对吧?)

ID(891217)->出生日期= 1989年12月17日

1989年12月17日->我想将其转换回字符串以保存到varchar数据类型的数据库中.

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.

推荐答案

看看TryParseExact 此处 [ ^ ].
Have a look at the TryParseExact method[^].

You can provide the exact format as one of the parameters of the function as describer here[^].


从日期格式转换为字符串的功能

Function for converting to string from date format

'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


这篇关于使用vb将String转换为Date并转换回String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆