给定以下2个字符串,注意其中的“.185”和“,185”:
2011-09-15 17:05:37,185
2011-09-15 17:05:37.185
从一个文件中读取(我无法控制),我可以看到它们有两种格式的日期。我需要创建一个函数来适应这两种情况。
“.”和“,”是特定于文化的吗?
有没有对于这样的功能的建议?
下面的代码不起作用,我得不到一个日期。
class Program
{
static void Main(string[] args)
{
string date1="2011-09-15 17:05:37.185";
string date2="2011-09-15 17:05:37,185";
const string format1 = "dd/MM/yyyy HH:mm:ss.ff";
const string format2 = "dd/MM/yyyy HH:mm:ss,ff";
DateTime resultDate1;
DateTime resultDate2;
DateTime.TryParseExact(date1, format1, CultureInfo.InvariantCulture, DateTimeStyles.None, out resultDate1);
DateTime.TryParseExact(date2, format2, CultureInfo.InvariantCulture, DateTimeStyles.None, out resultDate2);
Console.WriteLine(resultDate1.ToString());
Console.WriteLine(resultDate2.ToString());
Console.Read();
}
}
dd/MM...
和2011-09...
不匹配。 :) - bzlm