键入转换,日期格式为YYYYMMDD [英] Type Conversion with Date formatted as YYYYMMDD
问题描述
我有一个字符串变量,其中包含格式为YYYYMMDD的日期
例如 - Dim x as string =" 20070314"
如果我尝试执行类型转换如下我得到一个错误:
Dim y as Date = CDATE(x)
如何为日期执行类型转换格式YYYYMMDD?
谢谢。
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
How can I perform a type conversion for the date format YYYYMMDD?
Thanks.
推荐答案
3月14日晚上1点08分,Mike< M ... @ discussion.microsoft.comwrote:
On Mar 14, 1:08 pm, Mike <M...@discussions.microsoft.comwrote:
我有一个包含日期格式为YYYYMMDD的字符串变量
例如 - Dim x as string =" 20070314"
如果我尝试按如下方式执行类型转换,则会收到错误:
Dim y as Date = CDATE( x)
如何为日期格式YYYYMMDD执行类型转换?
谢谢。
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
How can I perform a type conversion for the date format YYYYMMDD?
Thanks.
私有函数ConvertDate(ByVal值为字符串)作为日期
''将YYYYMMDD格式的日期转换为MM / DD / YYYY格式。
如果不是value.Length = 8那么
抛出新的ArgumentException(无效长度)
结束如果
如果不是IsNumeric(值)则
抛出新的ArgumentException(不是有效的8位数字。)
结束如果
Dim theYear As String = value.SubString(0,4)
Dim theMonth As String = value.SubString(4 ,2)
Dim theDay As String = value.SubString(6,2)
Dim result As String = theMonth& " /" &安培; theDay& " /" &安培; theYear
如果不是IsDate(结果)那么
抛出新的ArgumentException(值无法转换为
有效日期。)
结束如果
返回CDate(结果)
结束功能
基本上,将它解析为它的组成部分,然后将它与
一起拼接成你想要的格式。我确定你可以用它来做一个常规的
表达式,但是我只需要维护程序员可以读取的清晰代码。另外,我会添加很多错误
检查验证参数。
希望这会有所帮助。
Mike
Private Function ConvertDate(ByVal value As String) As Date
'' Converts a date in YYYYMMDD format to MM/DD/YYYY format.
If Not value.Length = 8 Then
Throw New ArgumentException("Invalid length")
End If
If Not IsNumeric(value) Then
Throw New ArgumentException("Not a valid 8-digit number.")
End If
Dim theYear As String = value.SubString(0, 4)
Dim theMonth As String = value.SubString(4, 2)
Dim theDay As String = value.SubString(6, 2)
Dim result As String = theMonth & "/" & theDay & "/" & theYear
If Not IsDate(result) Then
Throw New ArgumentException("value cannot be converted to a
valid date.")
End If
Return CDate(result)
End Function
Basically, parse it into it''s constituent parts, then splice it back
together into the format you want. I''m sure there''s a regular
expression you could use to do it, but I''m all for clear code that
maintenance programmers can read. Plus, I get to add lots of error
checking that validates the argument.
Hope this helps.
Mike
绝对完美!非常感谢你的帮助,Mike。
Mike
" Mike Hofer"写道:
Absolutely Perfect! Thank you so very much for your assistance, Mike.
Mike
"Mike Hofer" wrote:
3月14日晚上1:08,Mike< M ... @ discussion.microsoft.comwrote:
On Mar 14, 1:08 pm, Mike <M...@discussions.microsoft.comwrote:
我有一个字符串变量,其中包含格式为YYYYMMDD的日期
例如 - Dim x as string =" 20070314"
如果我尝试按如下方式执行类型转换,则会出现错误:
Dim y as Date = CDATE(x)
如何执行类型转换为日期格式YYYYMMDD?
谢谢。
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
How can I perform a type conversion for the date format YYYYMMDD?
Thanks.
私有函数ConvertDate(ByVal值为字符串)作为日期
''将YYYYMMDD格式的日期转换为MM / DD / YYYY格式。
如果不是value.Length = 8那么
抛出新的ArgumentException(无效长度)
结束如果
如果不是IsNumeric(值)则
抛出新的ArgumentException(不是有效的8位数字。)
结束如果
Dim theYear As String = value.SubString(0,4)
Dim theMonth As String = value.SubString(4 ,2)
Dim theDay As String = value.SubString(6,2)
Dim result As String = theMonth& " /" &安培; theDay& " /" &安培; theYear
如果不是IsDate(结果)那么
抛出新的ArgumentException(值无法转换为
有效日期。)
结束如果
返回CDate(结果)
结束功能
基本上,将它解析为它的组成部分,然后将它与
一起拼接成你想要的格式。我确定你可以用它来做一个常规的
表达式,但是我只需要维护程序员可以读取的清晰代码。另外,我会添加很多错误
检查验证参数。
希望这会有所帮助。
Mike
Private Function ConvertDate(ByVal value As String) As Date
'' Converts a date in YYYYMMDD format to MM/DD/YYYY format.
If Not value.Length = 8 Then
Throw New ArgumentException("Invalid length")
End If
If Not IsNumeric(value) Then
Throw New ArgumentException("Not a valid 8-digit number.")
End If
Dim theYear As String = value.SubString(0, 4)
Dim theMonth As String = value.SubString(4, 2)
Dim theDay As String = value.SubString(6, 2)
Dim result As String = theMonth & "/" & theDay & "/" & theYear
If Not IsDate(result) Then
Throw New ArgumentException("value cannot be converted to a
valid date.")
End If
Return CDate(result)
End Function
Basically, parse it into it''s constituent parts, then splice it back
together into the format you want. I''m sure there''s a regular
expression you could use to do it, but I''m all for clear code that
maintenance programmers can read. Plus, I get to add lots of error
checking that validates the argument.
Hope this helps.
Mike
Mike写道:
Mike wrote:
我有一个字符串变量包含格式为YYYYMMDD的日期
例如 - Dim x as string =" 20070314"
如果我尝试按如下方式执行类型转换,我会得到一个错误:
Dim y as Date = CDATE(x)
I have a string variable containing a date formatted as YYYYMMDD
For example - Dim x as string = "20070314"
If I try to perform a type conversion as follows I get an error:
Dim y as Date = CDATE(x)
< snip>
我想你会很高兴知道Date类有一个共享方法
这样做:
Dim D As Date = Date .ParseExact(X," yyyyMMdd",Nothing)
HTH。
问候,
Branco。
<snip>
I guess you''ll be glad to know that the Date class has a Shared method
to do just that:
Dim D As Date = Date.ParseExact(X, "yyyyMMdd", Nothing)
HTH.
Regards,
Branco.
这篇关于键入转换,日期格式为YYYYMMDD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!