如何在asp.net C#中将文本框字符串转换为日期时间?
问题描述:
如何在asp.net c#中将文本框字符串转换为datetime?
How can I convert a textbox string to datetime in asp.net c# ?
我尝试过:
DateTime d2 = Convert.ToDateTime(tbx_Created.Text);
string createdformatted = d2.ToString("MM/dd/yyyy hh:mm:ss tt");
DateTime CreatdDate = DateTime.ParseExact(tbx_Created.Text, "MM/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
但它显示此错误:
字符串未被识别为有效的DateTime
String was not recognized as a valid DateTime
我在文本框中输入了15-6-2016.
I have given 15-6-2016 to textbox.
请告知.
答
您可以这样解析用户输入:
You can parse user input like this:
DateTime enteredDate = DateTime.Parse(enteredString);
如果该字符串具有特定格式,则应使用另一种方法:
If you have a specific format for the string, you should use the other method:
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);
您输入的格式应与确切"相符:
Your formats input should match the Exact:
DateTime.ParseExact("24/01/2013", "dd/MM/yyyy");