[英]How to use C# DateTime.TryParse (easiest way possible)
我在win7 pt-BR机器上使用了Visual Studio 2012,但也需要此代码在Win2003ServerR2 en-US Server上工作。
出现以下错误:
无法将字符串识别为有效的DateTime。 System.DateTimeParse.ParseExact(String s,String格式,DateTimeFormatInfo dtfi,DateTimeStyles样式)位于System.DateTime.ParseExact(String s,字符串格式,IFormatProvider提供程序)位于AppCombustivel.Service1.TabelaConsumos()系统上的System.Collections.ListDictionaryInternal mscorlib .DateTime ParseExact(System.String,System.String,System.Globalization.DateTimeFormatInfo,System.Globalization.DateTimeStyles)
我的ShortDate服务器配置:
yyyy/MM/dd
我的代码:
string MyString: return the result of "SELECT MAX(Data_Column) FROM Table_Data T" (the last date registered in my BD)
MyString可以像:
13/01/2013 00:00:00
或
01/15/2013 00:00:00
或
2013/02/30 00:00:00
,我不知道,这取决于每个操作系统的语言和短日期格式。 我只需要MyString的Date,来自MyString的时间信息并不重要* /
DateTime dateIwillUse;
if (DateTime.TryParse("20/01/2013", out dateIwillUse))
{
Console.WriteLine("This System uses short Date in format: dd/MM/yyyy. Parsing in correct format to dateIwillUse:");
dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "dd/MM/yyyy", CultureInfo.CurrentCulture);
//using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
}
else if (DateTime.TryParse("01/20/2013", out dateIwillUse))
{
Console.WriteLine("This system uses short date in format: MM/dd/yyyy.Parsing in correct format to dateIwillUse:");
dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "MM/dd/yyyy", CultureInfo.CurrentCulture );
//using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
}
else if (DateTime.TryParse("2013/01/20", out dateIwillUse))
{
Console.WriteLine("This system uses short date in format: yyyy/MM/dd .Parsing in correct format to dateIwillUse:");
dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "yyyy/MM/dd", CultureInfo.CurrentCulture);![enter image description here][2]
//using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
}
else if (DateTime.TryParse("01/20/2013", out dateIwillUse))
{
Console.WriteLine("This system uses short date in format: MM/dd/yyyy .Parsing in correct format to dateIwillUse:");
dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "MM/dd/yyyy", CultureInfo.CurrentCulture);
//using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
} else
{
Console.WriteLine("Cannot determine the format. Is not MM/dd/yyyy, neither yyyy/MM/dd, or dd/MM/yyyy");
}
为什么在检索sql时不强制使用特定的日期格式? 这样,您的代码将知道字符串的格式。没有理由编写这么多解析器。
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
编辑:添加了更多信息
只有您的数据库知道日期的正确格式。 如果您尝试使用不同的解析器,则日期01-05-2013是2013年1月5日或2013年5月1日,两者都是有效日期,将进行解析,但是它们的含义非常不同,请注意。
DateTime.Parse具有重载,可让您基于日期的格式进行解析。 http://msdn.microsoft.com/zh-cn/library/system.datetime.parse(v=vs.110).aspx#Parse2_Example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.