如何在asp.net C#中将文本框字符串转换为日期时间? [英] How to convert a Textbox String to a Datetime in asp.net c#?
本文介绍了如何在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");
这篇关于如何在asp.net C#中将文本框字符串转换为日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文