字符串到DateTime [英] String to DateTime

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

问题描述

我正在尝试从文本文件中读取一行并将其存储在DateTime

中。下面是我的问题代码的缩减版本(在我的

实际版本中我正在使用try和catch,我收到每个

行在一个循环中存储进入数组)


如果我将字符串直接写入数组它可以工作,但是当我使用

来自文件的行时它不会进入DateTime可靠,我

得到一个错误。有什么我想念的吗?


Dim inputArray(2)As String

FileOpen(1," F:/DestinationTime.txt" OpenMode。输入)

inputArray(2)= LineInput(1)

FileClose(1)


Dim temp As DateTime = CDate( inputArray(2))''问题行


文件中的行如下:

9/14/2004 6:00:00 PM
是的我累了添加#s。

我甚至累了选项明确关闭


我得到的错误是:

发生了'System.InvalidCastException'类型的未处理异常
microsoft.visualbasic.dll中的



附加信息:从字符串转换9/14/2004 6:00:00 PM

类型''日期''无效。


我敢打赌,这很简单。

I am trying to read a line from a text file and store it in a DateTime
veritable. Below is a cut down version of my problem code (in my
actual version I am using a try and catch, and I am receiving each
line in a loop to store into the Array)

If I write the string directly into the array it works, but when I use
the line from the file it doesn''t go into the DateTime veriable and I
get an error. Is there something I am missing?

Dim inputArray(2) As String
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)
inputArray(2) = LineInput(1)
FileClose(1)

Dim temp As DateTime = CDate(inputArray(2)) ''Problem Line

The line in the file reads:
9/14/2004 6:00:00 PM
And yes I have tired adding #s.
And I even tired Option Explicit Off

The error I get is:
An unhandled exception of type ''System.InvalidCastException'' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "9/14/2004 6:00:00 PM" to
type ''Date'' is not valid.

I bet it''s something dead simple.

推荐答案

koorb< ko *** @ raidrs.co.uk>写在

新闻:sm ******************************** @ 4ax.com:
koorb <ko***@raidrs.co.uk> wrote in
news:sm********************************@4ax.com:
我得到的错误是:
在microsoft.visualbasic.dll中发生了'System.InvalidCastException'类型的未处理异常


附加信息:从字符串9/14/2004 6:00:00 PM投射
输入''日期''无效。
The error I get is:
An unhandled exception of type ''System.InvalidCastException'' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "9/14/2004 6:00:00 PM" to
type ''Date'' is not valid.



根据文件:


CDate根据文件识别日期格式

系统的区域设置。您必须以正确的顺序提供日期,月份和年份

您的语言环境,或者日期可能无法正确解释。

我看到你是在英国和你使用的日期是美国格式。

可能是因为CDate试图将第14天的第9天转换为b $ b月份有效日期时间 - 这是一个无效的演员例外。


尝试将您的计算机更改为美国日期时间?



-

Lucas Tam(RE********@rogers.com)

请删除删除来自回复时的电子邮件地址。
http://成员。 ebay.com/aboutme/coolspot18/


2004年8月31日星期二20:48:14 GMT,Lucas Tam< RE ***** ***@rogers.com>

写道:
On Tue, 31 Aug 2004 20:48:14 GMT, Lucas Tam <RE********@rogers.com>
wrote:
koorb< ko *** @ raidrs.co.uk>在
新闻中写道:sm ******************************** @ 4ax.com:
koorb <ko***@raidrs.co.uk> wrote in
news:sm********************************@4ax.com :
我得到的错误是:
在microsoft.visualbasic.dll中发生了'System.InvalidCastException'类型的未处理异常

附加信息:从字符串9/14/2004 6:00:00 PM转换而来
输入''日期''无效。
The error I get is:
An unhandled exception of type ''System.InvalidCastException'' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "9/14/2004 6:00:00 PM" to
type ''Date'' is not valid.



根据文档:

CDate根据语言环境设置识别日期格式你的
系统。您必须以正确的顺序为您的区域设置提供日期,月份和年份,否则日期可能无法正确解释。

我看到您在英国和您使用的日期是美国格式。
可能是CDate正试图将第14个月的第9天转换为有效的日期时间 - 这是一个无效的演员例外。

尝试将计算机更改为美国日期时间?


According to the documentation:

CDate recognizes date formats according to the locale setting of your
system. You must provide the day, month, and year in the correct order
for your locale, or the date may not be interpreted correctly.
I see that you''re in the UK and the date you''re using is in US format.
It could be that CDate is trying to convert the 9th day of the 14th
month into a valid date time - which is an invalid cast exception.

Try changing your computer to US Date Times?




谢谢,它有效!

然而当我输入时直接推荐的DateTime,我必须使用US!



Thankyou, it works!
And yet when I input a DateTime veritable directly, I have to use US!


* koorb< ko *** @ raidrs.co.uk> scripsit:
* koorb <ko***@raidrs.co.uk> scripsit:
FileOpen(1," F:/DestinationTime.txt",OpenMode.Input)
FileOpen(1, "F:/DestinationTime.txt", OpenMode.Input)




" /" - > " \"。

-

MS Herfried K. Wagner

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

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



"/" -> "\".

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


这篇关于字符串到DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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