简体   繁体   English

将字符串日期和时间转换为DateTime

[英]Convert a string Date and Time to a DateTime

I have to run through thousands of Word document and create XML files out of them. 我必须遍历数千个Word文档并从中创建XML文件。 Everything works fine except the Date fields because I'm working in two languages. 除日期字段外,其他所有东西都可以正常工作,因为我使用的是两种语言。

Here are a few examples 这里有一些例子

  • DATE: NOVEMBER 24, 2016 TIME: 15:31 日期:2016年11月24日时间:15:31
  • DATE: 28 NOVEMBRE 2016 HEURE: 10H31 日期:2016年11月28日时长:10H31

I cleanup up the string a bit using the below but I still get the infamous 'String was not recognized as a valid DateTime.' 我使用下面的代码清理了一下字符串,但是仍然得到臭名昭著的“字符串未被识别为有效的DateTime”。

IFormatProvider culture = null;
if (m.rdoEnglish.IsChecked == true)
{
    culture = new System.Globalization.CultureInfo("en-CA", true);
}
else if (m.rdoFrench.IsChecked == true)
{
    culture = new System.Globalization.CultureInfo("fr-CA", true);
}

string dt = "";
dt = m.txtPublished.Text;

if (dt.IndexOf("HEURE:") != -1)
{
    dt = dt.Replace("HEURE:", "");
}

if (dt.IndexOf("H") != -1)
{
    dt = dt.Replace("H", ":");
}
DateTime dt2;
dt2 = DateTime.ParseExact(dt, "MM/dd/yyyy HH:mm:ss tt", culture);

//Cleaned string looks like this " 28 NOVEMBRE 2016 10:31  "
return dt2;

Looks like your format string is incorrect, in light of the two examples you gave. 根据您给出的两个示例,看来您的格式字符串不正确。 It should be something like "MMMM dd, yyyy hh:mm" Read more about it at: DateTime.ParseExact Method 它应该类似于“ MMMM dd,yyyy hh:mm”。有关更多信息,请访问: DateTime.ParseExact方法

The ParseExact, as well TryParseExact, has an overload that accepts an array of formats to use in parsing the string. ParseExact和TryParseExact具有重载,该重载接受用于解析字符串的格式数组。 This will allow you to use something like this 这将允许您使用类似这样的东西

string test = dt.Replace("DATE: ", "")
                .Replace("TIME: ", "")
                .Replace("HEURE: ", "");

string[] formats = new string[]
{
    "MMMM dd, yyyy HH:mm", "dd MMMM yyyy HH\'H\'mm"
};
DateTime dt2 = DateTime.ParseExact(test, formats, culture, DateTimeStyles.None);

Notice that I don't try to replace the single char H inside the french version of your string because I don't know if the same char appears somewhere in your months. 请注意,我不尝试在法语字符串的字符串中替换单个char H,因为我不知道在您的月份中是否出现相同的char。

If the strings you need to parse can be in two different cultures in the same document then you need to code more defensively. 如果您需要解析的字符串可以在同一文档中处于两种不同的区域性,则您需要进行更具防御性的编码。

Set up both cultures: 建立两种文化:

IFormatProvider englishCulture = new System.Globalization.CultureInfo("en-CA", true);
IFormatProvider frenchCulture = new System.Globalization.CultureInfo("fr-CA", true);

Then use TryParse with one culture and if that fails try again with the other: 然后将TryParse与一种区域性结合使用,如果失败,则尝试另一种区域性:

DateTime output;
if (DateTime.TryParseExact(input, frenchFormatString, frenchCulture, out output))
{
    // Read it successfully
}
else if (DateTime.TryParseExact(input, englishFormatString, englishCulture, out output))
{
    // Read it successfully 
}
else
{
    // Unknown format, try something else?
}

If you have information that the text should be in French or English you could arrange the code so that the most likely culture is tried first. 如果您知道该文本应为法语或英语,则可以安排代码,以便首先尝试最有可能的区域性。

The format strings can contain all the padding words as well as the placeholders for the actual data. 格式字符串可以包含所有填充字以及实际数据的占位符。 There's even an overload of TryParseExact that takes an array of format strings so you can set up all (or at least most) of the formats you'll encounter. 甚至有一个TryParseExact重载,它需要一组格式字符串,因此您可以设置将遇到的所有(或至少大多数)格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM