<分区>
我喜欢将字符串“May 01 2000”转换为 DateTime
我试过下面这段代码,但我得到了一个错误
string date = "May 01 2000";
DateTime DT=Convert.ToDateTime(date)
标签 c# asp.net-mvc
<分区>
我喜欢将字符串“May 01 2000”转换为 DateTime
我试过下面这段代码,但我得到了一个错误
string date = "May 01 2000";
DateTime DT=Convert.ToDateTime(date)
最佳答案
string s = "May 01 2000";
DateTime dt = DateTime.ParseExact(s, "MMM dd yyyy", CultureInfo.InvariantCulture);
但是,您几乎总是最好使用DateTime.TryParse()
因为如果转换失败它不会抛出异常:
Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.
string s = "May 01 2000";
DateTime dateValue;
if (DateTime.TryParse(s, out dateValue) == true)
{
// succeeded ...
}
关于c# - 字符串到日期转换C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18820402/