本文介绍了日期类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串转换为日期,但是如何解决此问题会出现错误?
错误:从字符串"到类型日期"的转换无效.
该字符串采用dd/mm/yyyy格式,并且我使用了cdate和convert.todatetime函数,但在两种情况下转换均未成功完成.
I am trying to convert string to date but the error would occur how to solve this problem?
error : Conversion from string "" to type ''Date'' is not valid.
The string is in dd/mm/yyyy format and and I have used cdate and convert.todatetime functions but in both case conversion was not success-full.
推荐答案
DateTime.ParseExact("18-12-2007", "dd-MM-yyyy", CultureInfo.InvariantCulture)<br />
这是处理您使用的非标准日期格式所必需的.
This is needed to handle the date format you are using which is not a standard one.
<pre></pre>string datestring = "20000101";<br />
string date1 = DateTime.ParseExact(datestring, yyyyMMdd, null);<br />
// ... or ...<br />
DateTime dateResult;<br />
if (!DateTime.TryParseExact(datestring, yyyyMMdd,<br />
null, DateTimeStyles.AssumeLocal,<br />
out dateResult))<br />
dateResult = DateTime.MinValue;
也请查看这篇文章
http://www.codeproject.com/KB/cs/String2DateTime.asp [ ^ ]
Check this article too
http://www.codeproject.com/KB/cs/String2DateTime.asp[^]
这篇关于日期类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!