日期转换失败错误
我有一个变量'v_startdt'作为静态日期时间,'v_expdt'作为静态字符串。
我以dd / MM / yyyy格式在TextBox4 thro日历扩展器中获取日期。然后我将其分配给v_startdt以添加某些日期并将结果存储在v_expdt中。稍后我使用这个v_expdt插入'date'数据类型的exp_date列中的表格,格式为'dd / MM / yyyy'。
以下是我的代码:
v_expdt = v_startdt.AddDays(v_adddays).ToString(dd / MM / yyyy);
我得到的错误就像 - '从字符串转换日期时转换失败'。
I have a variable 'v_startdt' as static datetime and 'v_expdt' as static string.
I get date in TextBox4 thro calender extender in dd/MM/yyyy format. I then assign this to v_startdt to add somedays and store that result in v_expdt. Later I use this v_expdt to insert into a table in exp_date column of 'date' datatype in 'dd/MM/yyyy' format.
Hereunder is my code:
v_expdt = v_startdt.AddDays(v_adddays).ToString("dd/MM/yyyy");
I get the error like -'Conversion failed when converting date from character string'.
请参阅我的解决方案(解决方案2),了解类似错误的previuos帖子。
在c#asp.net中将字符串转换为日期时间 [ ^ ]
See my solution (Solution 2) for the previuos post on similar error.
Conversion of string to date time in c# asp.net[^]
你的问题是错的:你不可能从显示的代码行中得到这个例外。它将System.DateTime
转换为字符串,而不是将字符串解析为System.DateTime
。您应该学习如何使用调试器并正确报告问题。你的问题出在其他地方。
不用担心:只需学习方法Parse
,ParseExact
,TryParse
等:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29。 aspx [ ^ ]。
如您所见,您需要了解文化的使用和格式:
http://msdn.microsoft.com/en-us/ library / system.iformatprovider(v = vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/ library / az4se3k1(v = vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v = vs.110).aspx [ ^ ]。-SA
Your question is wrong: you cannot possibly get this exception from the line of code shown. It convertsSystem.DateTime
to string, not parses the string toSystem.DateTime
. You should learn how to use the debugger and report problems properly. Your problem is somewhere else.
Not to worry: just learn the methodsParse
,ParseExact
,TryParse
, etc.:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^].
As you can see, you need to understand the use of culture and also formatting:
http://msdn.microsoft.com/en-us/library/system.iformatprovider(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx[^].—SA
检查以下链接以添加天数。使用C#
DateTime。 AddDays方法
C#DateTime
Check following links to add days. using C#
DateTime.AddDays Method
C# DateTime