FieldConverter ConverterKind.Date“dd/MM/yyyy”例外

发布于 2024-10-02 17:32:25 字数 1449 浏览 5 评论 0原文

我尝试读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-10-09 17:32:25

它失败的原因是自定义日期格式字符串中的 / 是 一种文化-具体的 DateSeparator 如 MSDN 中所述。

您为 IFormatProvider 参数指定 null,因此它使用当前区域性,该区域性可能具有除 / 之外的日期分隔符。

最好的解决方案是显式指定 CultureInfo.InvariantCulture(下面的第二个版本)。转义自定义日期格式字符串中的“/”,以便将其视为文字斜杠而不是日期分隔符也将起作用(下面的第一个版本)。

// 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);

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 the IFormatProvider 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).

// 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);
永不分离 2024-10-09 17:32:25

当你尝试时会发生什么:

DateTime.TryParseExact(from, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);

What happens when you try:

DateTime.TryParseExact(from, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文