.Net 2.0中的Convert.DateTime错误 [英] Convert.DateTime Error in .Net 2.0
问题描述
大家好
当我将字符串转换为
日期时间时,我收到了一个非常棒的错误:
代码为:
DateTime dt1 = Convert.ToDateTime(" 10 Sep 2005");
Console.WriteLine(dt1.Year );
我在xp Pro SP2上安装了.Net 2.0。当我执行以下操作时,我得到
错误说:
这是什么?多么奇怪。至关重要的是我可以将字符串
转换为日期 - 任何人都知道从哪里开始这个?我在vb中尝试了它并且
它仍然抛出相同的错误。
System.IndexOutOfRangeException未处理
消息="索引超出了数组的范围。
Source =" mscorlib"
StackTrace:
at
System.Globalization.DateTimeFormatInfo.InsertHash(TokenHashValue []
hashTable,String str,TokenType tokenType,Int32 tokenValue)
at
系统.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
在System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask,TokenType& tokenType,Int32& tokenValue,__DTString& str)
at System .__ DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps,__DTString& str,
DateTimeToken& ; dtok,DateTimeRawInfo& raw,DateTimeResult& result,
DateTimeFormatInfo& dtfi)
在System.DateTimeParse.T ryParse(String s,DateTimeFormatInfo
dtfi,DateTimeStyles styles,DateTimeResult& System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,
DateTimeStyles样式)
在System.DateTime.Parse(String) s,IFormatProvider提供者)
at System.Convert.ToDateTime(String value)
at tmpWin1.Form1.Form1_Load(Object sender,EventArgs e)in D:\ My
Stuff \Programming\Net 2.0 \ C#\ tmp \tmpWin1 \ tmpWin1 \tmpWin1 \Form1.cs:line
20
在System.Windows.Forms.Form.OnLoad(EventArgs e)
在System.Windows.Forms.Form.OnCreateControl()
在System。 Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
在System.Windows.Forms.Control.CreateControl()
在System。 Windows.Forms.Control.WmShowWindow(消息& m)
在System.Windows.Forms.Control.WndProc(消息& m)
在System.Windows.Forms。 ScrollableControl.WndProc(Mes sage& m)
在System.Windows.Forms.ContainerControl。 WndProc(Mess age& m)
在System.Windows.Forms.Form.WmShowWindow(Message& m)
在System.Windows.Forms.Form.WndProc(消息& m)
at
System.Windows.Forms.Control。 ControlNativeWindow.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
在System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
在System.Windows.Forms.SafeNativeMethods。 ShowWindow(HandleRef
hWnd,Int32 nCmdShow)
在System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
在System。 Windows.Forms.Form.SetVisibleCore(布尔值)
在System.Windows.Forms.Control.set_Visible(布尔值)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason,ApplicationContext context)
System.Windows.Forms。 Application.ThreadContext.Run MessageLoop(Int32
reason,ApplicationC ontext context)
在System.Windows.Forms.Application.Run(Form mainForm)
at tmpWin1.Program.Main()in D:\ My Stuff \编程\ Net
2.0 \C#\ tmp \tmpWin1 \ tmpWin1 \ tmpWin1 \Program.cs:第17行
在System.AppDomain.nExecuteAssembly (程序集程序,String []
args)
在System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity,String [] args )
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
executionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
发生这种情况
Hi all
I am getting a really bizzare error on when I convert a string into a
datetime:
The code is :
DateTime dt1 = Convert.ToDateTime("10 Sep 2005");
Console.WriteLine(dt1.Year);
I have installed .Net 2.0, on xp Pro SP2. When I do the following I get
an error saying:
What is this? How strange. Its pretty vital that I can convert strings
to dates - anyone know where to start with this? I tried it in vb and
it still threw the same error.
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"
StackTrace:
at
System.Globalization.DateTimeFormatInfo.InsertHash (TokenHashValue[]
hashTable, String str, TokenType tokenType, Int32 tokenValue)
at
System.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps, __DTString& str,
DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result,
DateTimeFormatInfo& dtfi)
at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo
dtfi, DateTimeStyles styles, DateTimeResult& result)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at tmpWin1.Form1.Form1_Load(Object sender, EventArgs e) in D:\My
Stuff\Programming\Net 2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Form1.cs:line
20
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at tmpWin1.Program.Main() in D:\My Stuff\Programming\Net
2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
This happens
推荐答案
嗯,这不是约会......也不是约会。 2006年9月10日。
Well, that''s not a date... neither is "The Tenth of September 2006".
尝试
DateTime dt1 = DateTime.Parse(" 10 Sep 2005");
" Me" < IL *** @ igsoftwaresolutions.co.uk>在消息中写道
新闻:11 ********************** @ z34g2000cwc.googlegr oups.com ...
try
DateTime dt1 = DateTime.Parse("10 Sep 2005");
"Me" <il***@igsoftwaresolutions.co.uk> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
大家好
当我将字符串转换为
日期时间时,我收到了一个非常奇怪的错误:
代码是:
DateTime dt1 = Convert.ToDateTime(" 2005年9月10日);
Console.WriteLine(dt1.Year);
我已安装.Net 2.0,在xp Pro SP2上。当我做以下操作时,我得到一个错误说:
这是什么?多么奇怪。将字符串转换为日期非常重要 - 任何人都知道从哪里开始这个?我在vb中尝试了它并且它仍然犯了同样的错误。
System.IndexOutOfRangeException未处理
Message ="索引超出了数组的范围。"
Source =" mscorlib"
StackTrace:
在System.Globalization.DateTimeFormatInfo.InsertHash(TokenHashValue []
hashTable,String str,TokenType tokenType,Int32 tokenValue)
在System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask,TokenType& tokenType,Int32& tokenValue,__DTString& str。)系统.__ DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
在System.DateTimeParse.Lex(DS dps,__DTString& str,
DateTimeToken& dtok,DateTimeRawInfo& raw,DateTimeResult& result System.DateTimeParse.TryParse(String s,DateTimeFormatInfo
dtfi),
DateTimeFormatInfo& dtfi)
,DateTimeStyles样式,DateTimeResult& System.DateTimeParse.Parse(String s,DateTimeFormatInfo dtfi,
DateTimeStyles样式)
在System.DateTime.Parse(String s,IFormatProvider provider)
在System。 Convert.ToDateTime(String value)
在tmpWin1.Form1.Form1_Load(Object sender,EventArgs e)中的D:\ My
Stuff \Programming\Net 2.0 \ C#\ tmp \在System.Windows.Forms.Form上的System.Windows.Forms.Form.OnLoad(EventArgs e)中,tmpWin1 \ tmpWin1 \tmpWin1 \Form1.cs:line
20
。 System.Windows上的System.Windows.Forms.Control.CreateControl()处于System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)的OnCreateControl()
。在System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)的System.Windows.Forms.Control.WndProc(Message& m)上的Forms.Control.WmShowWindow(Message& m)
在System.Windows.Forms.ContainerControl.WndProc上(/)混乱时代& m)
在System.Windows.Forms.Form.WmShowWindow(消息& m)
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)> at /> System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
在System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd,Int32 msg,IntPtr在System.Windows.Forms.Control.SetVisibleCore(Boolea n value)的System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd,Int32 nCmdShow)
上的wparam,IntPtr lparam)在System.Windows.Forms.Form.SetVisibleCore(布尔值)
在System.Windows.Forms.Control.set_Visible(布尔值)
在System.Windows.Forms上。 Application.ThreadContext.Run MessageLoopInner(Int32
reason,ApplicationContext context)
在System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason,ApplicationContext context)<在/ System.Windows.Forms.Application.Run(Form mainForm)
在tmpWin1.Program.Main()中的D:\ My Stuff \Programming\Net
2.0 \ C#\ tmp \\在System.AppDomain.ExecuteAssembly上的System.AppDomain.nExecuteAssembly(Assembly assembly,String []
args)上的\\ tmpWin1 \tmpWin1 \tmpWin1 \Program.cs:第17行
字符串assemblyFile,
证据assemblySecurity,String [] args)
在System.Threading.ThreadHelper.ThreadStart_Context(Object)中的Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
状态)
在System.Threading.ExecutionContext.Run(ExecutionCon文本
executionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
这种情况发生
Hi all
I am getting a really bizzare error on when I convert a string into a
datetime:
The code is :
DateTime dt1 = Convert.ToDateTime("10 Sep 2005");
Console.WriteLine(dt1.Year);
I have installed .Net 2.0, on xp Pro SP2. When I do the following I get
an error saying:
What is this? How strange. Its pretty vital that I can convert strings
to dates - anyone know where to start with this? I tried it in vb and
it still threw the same error.
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="mscorlib"
StackTrace:
at
System.Globalization.DateTimeFormatInfo.InsertHash (TokenHashValue[]
hashTable, String str, TokenType tokenType, Int32 tokenValue)
at
System.Globalization.DateTimeFormatInfo.CreateToke nHashTable()
at System.Globalization.DateTimeFormatInfo.Tokenize(T okenType
TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
at System.__DTString.GetSeparatorToken(DateTimeFormat Info dtfi)
at System.DateTimeParse.Lex(DS dps, __DTString& str,
DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result,
DateTimeFormatInfo& dtfi)
at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo
dtfi, DateTimeStyles styles, DateTimeResult& result)
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi,
DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider)
at System.Convert.ToDateTime(String value)
at tmpWin1.Form1.Form1_Load(Object sender, EventArgs e) in D:\My
Stuff\Programming\Net 2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Form1.cs:line
20
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ContainerControl.WndProc(Mess age& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow( HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolea n value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at tmpWin1.Program.Main() in D:\My Stuff\Programming\Net
2.0\C#\tmp\tmpWin1\tmpWin1\tmpWin1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
This happens
当然是。 Convert.ToDateTime方法总是以各种格式重新计算
日期,特别是2004年9月10日。等等
这是它在.Net 1.1和1.0中运行的方式,但出于某种原因
2.0 apears是不同的,我迷路了为什么这是
Of course it is. The Convert.ToDateTime method has always reconised
dates in various formats, in particular "10 Sep 2004" etc
This is the way it operated in .Net 1.1 and 1.0, but for some reason
2.0 apears to be different and I am lost as to why this is
这篇关于.Net 2.0中的Convert.DateTime错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!