问题描述
大家好,
我正在制作一个网站,其中有一个从客户端计算机获取的datetime字段,我尝试了所有解析方法,更改了日期区域,但仍然收到字符串未被识别为有效的Date Time"错误.
我已经应用了这些方法:
Hello everyone,
I am making one website in which there is a datetime field which takes from client machine, i have tried all methods of parsing, changing the date culture but still i am getting error of "String was not recognized as valid Date Time".
I have applied these methods :
1.System.DateTime str_date=DateTime.Parse(sel3.Text,System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat);
2. System.DateTime str_date=DateTime.Parse(sel3.Text,System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);
3. DateTime str_date=DateTime.Today;
我已经使用了所有解析方法,但是它们都不起作用
请帮我解决这个错误!
预先感谢,
问候,
Krunal panchal
I have used all parsing methods , but none of them worked
Please help me to resolve this error !!
Thanks in advance,
Regards,
Krunal panchal
推荐答案
System.Globalization.CultureInfo.GetCultureInfo("en-AU").DateTimeFormat
代替System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat
in stead of System.Globalization.CultureInfo.CreateSpecificCulture("en-AU").DateTimeFormat
string date1 = "11.25.2011 12:10:30";
DateTime date2;
if (DateTime.TryParseExact(date1, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date2))
{ Response.Write(date2.ToString("MM/dd/yyyy"));
}
else
{
Response.Write("Parsing failed!");
}
public DateTime ConvertStringToDateTime(string date, string dateFormat)
{
DateTime res;
CultureInfo provider = CultureInfo.InvariantCulture;
res = DateTime.ParseExact(date, dateFormat, provider);
return res;
}
并调用类似的函数,
and call the function like,
DateTime res = ConvertStringToDateTime("17/02/2012", "dd/MM/yyyy");
这篇关于字符串未被识别为有效的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!