奇怪的日期问题 [英] Strange Date Problem

查看:81
本文介绍了奇怪的日期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于变量dt =3/31/2007,为什么它会在某些机器上产生以下

异常?它在我的电脑上工作正常,但是其他人给我发了这个例外信息,因为它在他们的电脑上乱了。


DLDT.Columns(" Date" ;)。DataType = GetType(Date)

dr.Item(" Date")= Date.Parse(dt.Trim).ToShortDateString


系统.ArgumentException:该字符串未被识别为有效的DateTime。

无法存储< 3/31 / 2007in Date Column。预期类型是DateTime。 --->

System.FormatException:字符串未被识别为有效的DateTime。

解决方案

< blockquote> Terry Olsen写道:


鉴于变量dt =" 3/31/2007",为什么会产生以下结果

某些机器有例外吗?它在我的电脑上工作正常,但是其他人给我发了这个例外信息,因为它在他们的电脑上乱了。


DLDT.Columns(" Date" ;)。DataType = GetType(Date)

dr.Item(" Date")= Date.Parse(dt.Trim).ToShortDateString


系统.ArgumentException:该字符串未被识别为有效的DateTime。

无法存储< 3/31 / 2007in Date Column。预期类型是DateTime。 --->

System.FormatException:字符串未被识别为有效的DateTime。



日期格式未被数据库。当

转换它时,使用这种类型的日期格式

时很常见。


指定文化(解析字符串时,不要将
转换回字符串,然后再将其放入表中。


-

G?ran Andersson

_____
http ://www.guffa.com


特里,


你有没有试过过CDate的方法扩展的Net Library

Microsoft Visual Basic。

几乎所有的转换函数都不再与VB6有任何关系,但是

优化的网络。


Cor


" Terry Olsen" < to ****** @ hotmail.comschreef in bericht

news:OT ************** @ TK2MSFTNGP02.phx.gbl ...


鉴于变量dt =3/31/2007,为什么它会在某些机器上产生以下

异常?它在我的电脑上工作正常,但其他人已经发送了这个例外信息,因为它在他们的电脑上乱了。


DLDT.Columns(" Date" ;)。DataType = GetType(Date)

dr.Item(" Date")= Date.Parse(dt.Trim).ToShortDateString


系统.ArgumentException:该字符串未被识别为有效

DateTime。

无法存储< 3/31 / 2007in Date Column。预期类型是

DateTime。 --->

System.FormatException:字符串未被识别为有效的DateTime。



Cor Ligthert [MVP] < no ************ @ planet.nlschrieb:


你有没有尝试过来自扩展的Net Library的方法CDate

Microsoft Visual Basic。



''CDate''是一种语言功能,而不是图书馆功能。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http:// dotnet.mvps.org/dotnet/faqs/>


Given that variable dt = "3/31/2007", why does it produce the following
exception on some machines? It works fine on my PC, but others have sent me
this exception information because it threw up on their PC.

DLDT.Columns("Date").DataType = GetType(Date)
dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString

System.ArgumentException: The string was not recognized as a valid DateTime.
Couldn''t store <3/31/2007in Date Column. Expected type is DateTime. --->
System.FormatException: The string was not recognized as a valid DateTime.

解决方案

Terry Olsen wrote:

Given that variable dt = "3/31/2007", why does it produce the following
exception on some machines? It works fine on my PC, but others have sent me
this exception information because it threw up on their PC.

DLDT.Columns("Date").DataType = GetType(Date)
dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString

System.ArgumentException: The string was not recognized as a valid DateTime.
Couldn''t store <3/31/2007in Date Column. Expected type is DateTime. --->
System.FormatException: The string was not recognized as a valid DateTime.

The date format was not recognised by the database. This is common when
using this type of date format without specifying a culture when
converting it.

Specify a Culture (or FormatInfo) when parsing the string, and don''t
convert it back to a string before putting it in the table.

--
G?ran Andersson
_____
http://www.guffa.com


Terry,

Did you ever tried the method CDate from the extendened Net Library
Microsoft Visual Basic.
Almost all conversion functions have no relation anymore with VB6 but are
optimized Net ones.

Cor

"Terry Olsen" <to******@hotmail.comschreef in bericht
news:OT**************@TK2MSFTNGP02.phx.gbl...

Given that variable dt = "3/31/2007", why does it produce the following
exception on some machines? It works fine on my PC, but others have sent
me this exception information because it threw up on their PC.

DLDT.Columns("Date").DataType = GetType(Date)
dr.Item("Date") = Date.Parse(dt.Trim).ToShortDateString

System.ArgumentException: The string was not recognized as a valid
DateTime.
Couldn''t store <3/31/2007in Date Column. Expected type is
DateTime. --->
System.FormatException: The string was not recognized as a valid DateTime.



"Cor Ligthert [MVP]" <no************@planet.nlschrieb:

Did you ever tried the method CDate from the extendened Net Library
Microsoft Visual Basic.

''CDate'' is a language feature, not a library feature.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


这篇关于奇怪的日期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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