使用vb将String转换为Date并转换回String

编程入门 行业动态 更新时间:2024-10-11 23:25:40
本文介绍了使用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

本文发布于:2023-11-26 20:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   换回   并转   vb   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!