FieldConverter ConverterKind.Date“dd/MM/yyyy”例外
我尝试读取 csv 文件。 我的第五条记录包含日期: 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”转换为类型时出错:“DateTime”。使用格式:'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");
}
}
我得到了这个例外:无法从 03/11/2008 开始日期 Parameternaam:来自
我做错了什么?
I try to read a csv file.
my fifth record contans a date: 03/11/2008
This is a piece of my code:
[FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
public DateTime datum_5;
My code crashs on this:
Result[] results= (Result[])engine.ReadFile(@"..\Data\expo.txt");
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'
When i do this:
[FieldConverter(typeof(ConvertDate))]
public DateTime datum_5;
with this:
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");
}
}
I got this exception: can not make a date from 03/11/2008
Parameternaam: from
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它失败的原因是自定义日期格式字符串中的 / 是 一种文化-具体的 DateSeparator 如 MSDN 中所述。
您为
IFormatProvider
参数指定null
,因此它使用当前区域性,该区域性可能具有除 / 之外的日期分隔符。最好的解决方案是显式指定 CultureInfo.InvariantCulture(下面的第二个版本)。转义自定义日期格式字符串中的“/”,以便将其视为文字斜杠而不是日期分隔符也将起作用(下面的第一个版本)。
The reason it's failing is that / in a custom date format string is a culture-specific DateSeparator as described in MSDN.
You are specifying
null
for theIFormatProvider
argument, so it's using the current culture, which presumably has a date separator other than /.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).
当你尝试时会发生什么:
What happens when you try: