大家好! 谁能解决我的问题?我想将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
发布评论