如何在asp.net c#中将文本框字符串转换为日期时间?
如何在asp.net c#中将文本框字符串转换为日期时间 我试过这个:如何在asp.net c#中将文本框字符串转换为日期时间?,c#,asp.net,datetime,C#,Asp.net,Datetime,如何在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.Glob
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);
但它显示了这个错误:
字符串未被识别为有效的日期时间
我已将2016年6月15日交给textbox
请告知。对于“15-6-2016”
输入,日期时间模式应为“d-M-yyyy”
您可以尝试一次应用多个模式,如下所示:
DateTime CreatdDate = DateTime.ParseExact(tbx_Created.Text,
new String[] {
"MM/dd/yyyy hh:mm:ss tt", // your initial pattern, recommended way
"d-M-yyyy"}, // actual input, tolerated way
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal);
DateTime enteredDate = DateTime.Parse(enteredString);
对于“15-6-2016”
输入,日期时间模式应为“d-M-yyyy”
您可以尝试一次应用多个模式,如下所示:
DateTime CreatdDate = DateTime.ParseExact(tbx_Created.Text,
new String[] {
"MM/dd/yyyy hh:mm:ss tt", // your initial pattern, recommended way
"d-M-yyyy"}, // actual input, tolerated way
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal);
DateTime enteredDate = DateTime.Parse(enteredString);
您可以像这样解析用户输入:
DateTime CreatdDate = DateTime.ParseExact(tbx_Created.Text,
new String[] {
"MM/dd/yyyy hh:mm:ss tt", // your initial pattern, recommended way
"d-M-yyyy"}, // actual input, tolerated way
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal);
DateTime enteredDate = DateTime.Parse(enteredString);
如果您有字符串的特定格式,则应使用其他方法:
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);
您的输入格式应与以下内容完全匹配:
DateTime.ParseExact("24/01/2013", "dd/MM/yyyy");
您可以像这样解析用户输入:
DateTime CreatdDate = DateTime.ParseExact(tbx_Created.Text,
new String[] {
"MM/dd/yyyy hh:mm:ss tt", // your initial pattern, recommended way
"d-M-yyyy"}, // actual input, tolerated way
System.Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.AssumeLocal);
DateTime enteredDate = DateTime.Parse(enteredString);
如果您有字符串的特定格式,则应使用其他方法:
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);
您的输入格式应与以下内容完全匹配:
DateTime.ParseExact("24/01/2013", "dd/MM/yyyy");
您正在使用
MM
表示月份,而月份的值是6
而不是06
,因此您需要使用M
表示月份
DateTime dt = DateTime.Now;
DateTime.TryParseExact(tbx_Created.Text, "dd-M-yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
您正在对月份使用
MM
,而月份的值是6
而不是06
,因此您需要对月份使用M
DateTime dt = DateTime.Now;
DateTime.TryParseExact(tbx_Created.Text, "dd-M-yyyy", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
使用精确格式解析。但在解析之前,请检查它是否正在解析,并使用
使用精确格式解析。但在解析之前,请检查它是否正在解析,并使用 更好的方法是:
DateTime d2;
if (!DateTime.TryParse(tbx_Created.Text, out myDate))
{
// handle parse failure
}
更好的方法是:
DateTime d2;
if (!DateTime.TryParse(tbx_Created.Text, out myDate))
{
// handle parse failure
}
值得深思的是:
我给了textbox一个2016年6月15日的时间,DateTime.ParseExact(…,“MM/dd/yyyyy hh:MM:ss tt”)
注意到什么了吗?你正在传递一个日期,但试图解析一个时间,请删除除MM/dd/yyyyy
之外的所有内容。。我猜这也是@Manfred指出的:)值得思考:我给了textbox,DateTime.ParseExact(…,“MM/dd/yyyyy hh:MM:ss tt”)
注意到什么了吗?你正在传递一个日期,但试图解析一个时间,请删除除MM/dd/yyyyy
之外的所有内容。。我猜这也是@Manfred指出的:)