FieldConverter ConverterKind.Date“dd / MM / yyyy”例外 [英] FieldConverter ConverterKind.Date "dd/MM/yyyy" exception
问题描述
我尝试读取csv文件。
我的第五条记录包含日期:03/11/2008
I try to read a csv file. my fifth record contans a date: 03/11/2008
这是我的一段代码:
[FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
public DateTime datum_5;
我的代码在此崩溃:
Result[] results= (Result[])engine.ReadFile(@"..\Data\expo.txt");
并且有以下异常:
行:1.列:41.字段:datum_5。将'03 / 11/2008'转换为type:'DateTime'时出错。使用格式:'dd / MM / yyyy'
And with this exception: Line: 1. Column: 41. Field: datum_5. Error Converting '03/11/2008' to type: 'DateTime'. Using the format: 'dd/MM/yyyy'
当我这样做时:
[FieldConverter(typeof(ConvertDate))]
public DateTime datum_5;
:
internal class ConvertDate : ConverterBase
{
/// <summary>
/// different forms for date separator : . or / or space
/// </summary>
/// <param name="from">the string format of date - first the day</param>
/// <returns></returns>
public override object StringToField(string from)
{
DateTime dt;
if (DateTime.TryParseExact(from, "dd.MM.yyyy", null, DateTimeStyles.None, out dt))
return dt;
if (DateTime.TryParseExact(from, "dd/MM/yyyy", null, DateTimeStyles.None, out dt))
return dt;
if (DateTime.TryParseExact(from, "dd MM yyyy", null, DateTimeStyles.None, out dt))
return dt;
throw new ArgumentException("can not make a date from " + from, "from");
}
}
make a date from 03/11/2008
Parameternaam:from
I got this exception: can not make a date from 03/11/2008 Parameternaam: from
我做错了什么?
推荐答案
它失败的原因是/在自定义日期格式字符串如MSDN中所述的文化特定的DateSeparator 。
The reason it's failing is that / in a custom date format string is a culture-specific DateSeparator as described in MSDN.
您正在指定 null
为
IFormatProvider
参数,因此它使用当前的文化,可能有除了/.
You are specifying null
for the IFormatProvider
argument, so it's using the current culture, which presumably has a date separator other than /.
最好的解决方案是明确指定CultureInfo.InvariantCulture(下面的第二个版本)。转义自定义日期格式字符串中的'/',以便它被当作一个字符斜杠而不是一个DateSeparator也将工作(下面的第一个版本)。
The best solution is to explicitly specify CultureInfo.InvariantCulture (second version below). Escaping the '/' in your custom date format string so that it is treated as a literal slash rather than a DateSeparator will also work (first version below).
// Set current culture to a culture that uses "." as DateSeparator
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
// This will work - escaping means it uses a literal / as the separator
DateTime.TryParseExact(s, @"dd\/MM\/yyyy", null, DateTimeStyles.None, out result);
// This is better - Culture.InvariantCulture uses / for the DateTimeFormatInfo.DateSeparator
// and you clearly express the intent to use the invariant culture
DateTime.TryParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
// This will fail - / means use DateTimeFormatInfo.DateSeparator which is "." in the de-DE culture
DateTime.TryParseExact(s, "dd/MM/yyyy", null, DateTimeStyles.None, out result);
这篇关于FieldConverter ConverterKind.Date“dd / MM / yyyy”例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!