2010-11-16 15 views
5

Tôi cố gắng đọc tệp csv. kỷ lục thứ năm của tôi contans ngày: 03/11/2008FieldConverter ConverterKind.Date "dd/MM/yyyy" ngoại lệ

Đây là một đoạn mã của tôi:

[FieldConverter(ConverterKind.Date, "dd/MM/yyyy")] 
    public DateTime datum_5; 

đang crashs tôi về vấn đề này:

Result[] results= (Result[])engine.ReadFile(@"..\Data\expo.txt"); 

Và với ngoại lệ này: Dòng: 1. Cột: 41. Trường: datum_5. Lỗi khi chuyển đổi '03/11/2008 'thành loại:' Ngày giờ '. Sử dụng định dạng: 'dd/MM/yyyy'

Khi tôi làm điều này:

[FieldConverter(typeof(ConvertDate))] 

     public DateTime datum_5; 

với điều này:

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

     } 
    } 

tôi có ngoại lệ này: không thể làm cho một ngày từ 03/11/2008 Parameternaam: từ

Tôi đang làm gì sai?

Trả lời

5

Lý do nó không là/trong một chuỗi định dạng ngày tháng tùy chỉnh là a culture-specific DateSeparator as described in MSDN.

Bạn đang chỉ định null cho đối số IFormatProvider, do đó, nó sử dụng văn hóa hiện tại, có lẽ có dấu phân tách ngày khác với /.

Giải pháp tốt nhất là chỉ định rõ ràng CultureInfo.InvariantCulture (phiên bản thứ hai bên dưới). Thoát '/' trong chuỗi định dạng ngày tùy chỉnh của bạn để nó được coi là dấu gạch chéo thay vì một DateSeparator cũng sẽ hoạt động (phiên bản đầu tiên bên dưới).

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

Cảm ơn, nó hoạt động tại – meersmans

2

gì xảy ra khi bạn cố gắng:

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

gì nếu ghi:

[FieldConverter(ConverterKind.Date, "dd'/'MM'/'yyyy")] 
public DateTime datum_5;