0

我正在编写一个小程序,它获取 CSV 文件并将它们更新到数据库中。其中一个文件有一个date-of-birth列,并且该列的格式并不总是相同。

我已经开始编写代码来检查整个列表以确定格式,因为单个日期可能不明确(例如 '10/12/12'、'10/12/2012'、'12/10/2012'、'2012 /12/10' 可以是同一日期)。我假设给定列表的格式是一致的。

这是我到目前为止的代码,

private static string GetDateFormat(string[] date)
{
    DateTime result;
    CultureInfo ci = CultureInfo.InvariantCulture;
    string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();
    bool error;
    date = date.Where(x => !string.IsNullOrEmpty(x)).ToArray();
    foreach (string a in fmts)
    {
        error = false;
        for (int i = 0; i < date.Count(); i++)
        {
            if (!DateTime.TryParseExact(date[i], a, ci, DateTimeStyles.AssumeLocal, out result))
            {
                error = true;
            }

        }
        if (error == false)
        {
            return a;
        }            
    }
    throw new CsvToImsException("Error: Date Format is inconsistant or unrecognised");
}

'4/5/2012'但由于每个列表中的小问题(一个列表的日期设置为而不是'04/05/2012',另一个有'4/05/2012 0:00'等) ,我无法让它与我拥有的任何示例日期一起使用。

这一定是一个普遍的问题。有没有人为这个应用程序编写了足够广泛的库?我正在考虑按'/'字符拆分日期以进行解析,但是有没有更简单的方法?

4

2 回答 2

1

这里有一些东西可以让您走上您需要的正确轨道 请阅读示例代码中的注释,因为如果日期带有单月值和单日值,您只需要添加 2 个条件 if 语句

//of course you will not hard code the dates you will replace DateString with your 
//Date Variable you can also convert the code below into a method if you so 
string DateString = "04/05/2012";
var dateLength = DateString.Length;
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime dateVal;
switch (dateLength)
{
     case 8:
        {
          dateVal = DateTime.ParseExact(DateString, "M/d/yyyy", culture);
            break;
        }
    case 9:
        {
            // he you can add your own additional if(){} condition to check if date value Day has a length of 2 
            // if so then you know that the date is in m/dd/yyyy format
            // otherwise you know it's in mm/d/yyyy but 
            dateVal = DateTime.ParseExact(DateString, "M/dd/yyyy", culture);
            break;
        }
    case 10:
        {
            dateVal = DateTime.ParseExact(DateString, "MM/dd/yyyy", culture);
            break;
        }

}
于 2013-01-30T00:49:58.473 回答
0

我最终使用了一种类似于我原来的方法,并从 DJ KRAZE 的代码中添加了一些内容。这适用于除了真正奇怪的之外的所有4/05/2012 0:00情况,但即使是这样也可以通过添加特殊情况来解决fmts.Add("d/MM/yyyy h:mm")

//Parse DOB to check format
string[] dateList = new string[PersonList.Count()];
for (int i = 0; i < PersonList.Count(); i++)
{
    PersonList[i].DOB = PersonList[i].DOB.Replace('-', '/').Replace('.', '/').Trim();
    dateList[i] = PersonList[i].DOB;
}
string dateFormat = GetDateFormat(dateList);


private static string GetDateFormat(string[] date)
{
    DateTime result;
    CultureInfo ci = CultureInfo.InvariantCulture;
    List<string> fmts = ci.DateTimeFormat.GetAllDateTimePatterns().ToList();
    fmts.Add("yyyy/MM/d");
    fmts.Add("d/MM/yyyy");
    bool error;
    date = date.Where(x => !string.IsNullOrEmpty(x)).ToArray();
    foreach (string a in fmts)
    {
        error = false;
        for (int i = 0; i < date.Count(); i++)
        {
            if (!DateTime.TryParseExact(date[i], a, ci, DateTimeStyles.AssumeLocal, out result))
            {
                error = true;
            }
        }
        if (error == false)
        {
            return a;
        }          
    }
    throw new CsvToImsException("Error: Date Format is inconsistant or unrecognised");
}

然后我可以在 for 循环中使用日期格式来解析列表中的每个日期:

IFormatProvider culture = new CultureInfo("en-US", true);
BirthDate birthDate = DateTime.ParseExact(person.DOB, dateFormat, culture);
于 2013-01-30T05:52:36.157 回答