c#中的日期时间转换 [英] Date time Conversion in c#

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

问题描述

如果我编写此代码,则显示错误:

DateTime.TryParse(DTLocal.Rows [0] [Date]。ToString())

但以下内容未显示任何错误:

DateTime Date = DateTime.Parse(DTLocal.Rows [0] [Date]。ToString());

请解释这种转换的原因是什么。

If I write this code then it shows error :
DateTime.TryParse(DTLocal.Rows[0]["Date"].ToString())
But the following does not show any error :
DateTime Date=DateTime.Parse(DTLocal.Rows[0]["Date"].ToString());
Please explain what is the reason for such conversion.

推荐答案

你没有告诉我们你得到的错误,但是我很确定它是一个编译时间,因为TryParse期望第二个 out 绑定参数(参见:http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx [ ^ ])。

msdn手册页上有不同之处:

You have not told us the error you got, but I am pretty sure it is a compile time one, since TryParse is expecting a second out bound parameter (see: http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx[^]).
The difference is there in on the msdn manual page:
引用:

DateTime.TryParse(String,DateTime)方法类似于DateTime.Parse(String)方法,但如果转换失败,则TryParse(String,DateTime)方法不会抛出异常。

The DateTime.TryParse(String, DateTime) method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails.

请参阅此简短示例代码:

See this short sample code:

DateTime theDT1;
bool result1 = DateTime.TryParse("2013.01.01", out theDT1); // date is converted, resutl is true

DateTime theDT2;
bool result2 = DateTime.TryParse("not a date", out theDT2); // date is not converted, result is false

DateTime theDT3 = DateTime.Parse("not a date"); // date is not converted, exception thrown


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

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