[英]c# - Convert string to datetime with DateTime.ParseExact
I have to convert a string extract from a file to datetime. 我必须将文件的字符串提取转换为datetime。 The problem is that my string doesn't have a unique format. 问题是我的字符串没有唯一的格式。 For example my string can be something like : 19-05-2016 1:24:09:560 or 19-05-2016 21:24:09:56 or 19-05-2016 10:24:09:560 or 19-05-2016 10:24:09 and so on. 例如,我的字符串可以是:19-05-2016 1:24:09:560或19-05-2016 21:24:09:56或19-05-2016 10:24:09:560或19- 05-2016 10:24:09等等。 I didn't encounter all the possibilities yet ( these strings are pulled out of an json response from an API call) 我还没有遇到所有可能性(这些字符串是从API调用的json响应中提取出来的)
This is how my code looks like 这就是我的代码的样子
public static DateTime ConveDateTime(string a)
{
DateTime finished;
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:ff", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:f", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
try
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:ff", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
catch (Exception)
{
finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:f", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
}
}
}
}
}
}
return finished;
}
I want to know if there is any better solution in converting a string than the solution I have. 我想知道在转换字符串方面是否有比我的解决方案更好的解决方案。
DateTime.ParseExact has an overload that takes a string array of possible formats to use for parsing. DateTime.ParseExact 有一个重载 ,它接受一个可能的格式的字符串数组用于解析。 Use that overload and reduce your code to a line or two. 使用该重载并将代码减少到一行或两行。
string[] formats = new string[] {"dd-MM-yyyy HH:mm:ss:fff",
"dd-MM-yyyy H:mm:ss:fff",
"dd-MM-yyyy HH:mm:ss:f",
"dd-MM-yyyy HH:mm:ss",
....};
finished = DateTime.ParseExact(a, formats, CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal);
If you don't know all the possible formats you could also read them from an external file to avoid recompiling your application if a new format pops up 如果您不知道所有可能的格式,您也可以从外部文件中读取它们,以避免在弹出新格式时重新编译应用程序
Also, as said in the comments below, I prefer to use DateTime.TryParseExact to have more control on the outcome of the parsing and avoid a costly exception handling in case of a format not recognized. 另外,如下面的评论中所述,我更喜欢使用DateTime.TryParseExact来更好地控制解析的结果,并避免在格式无法识别的情况下进行昂贵的异常处理。
What about using DateTime.Parse() instead of DateTime.ParseExact()? 使用DateTime.Parse()而不是DateTime.ParseExact()怎么样? DateTime.Parse() is far more forgiving than ParseExact. DateTime.Parse()比ParseExact更宽容。 This way you don't have to define all the possible formats? 这样您就不必定义所有可能的格式了?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.