Category Archives: FileHelpers

Nullable Dates in FileHelpers

I was fighting problem where I hade a DateTime? and wanted the result to be an empty string if the value is null. By default, FileHelpers was giving me ‘01010001’.

This little custom converter took fine care of the problem:

 


    public class CustomDateConverter : ConverterBase
    {
        public override object StringToField(string value)
        {
            if(value == null)
            {
                return string.Empty;
            }
            return value;
        }
    }

The field being decorated looks like this:

   
[FieldConverter(typeof(CustomDateConverter))]  
public DateTime? VerificationDate;