日期类型转换 [英] date type conversion

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

问题描述

我正在尝试将字符串转换为日期,但是如何解决此问题会出现错误?

错误:从字符串"到类型日期"的转换无效.
该字符串采用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.

推荐答案

错误明确指出该字符串不是Null或Empty.选中String.IsNullOrEmpty(mystring),然后进行转换.会的.

UDPATE:
根据更改后的错误,尝试:
Error clearly state that the string is not Null or Empty. Put a check of String.IsNullOrEmpty(mystring), and then do the conversion. It would work.

UDPATE:
based on the changed error, try:
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.


请尝试使用DateTime.Parse(string)
希望对您有帮助..
try using DateTime.Parse(string)
hope this helps you..


<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[^]


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

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