Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datetime format #39

Closed
lafama opened this issue Jul 12, 2012 · 12 comments
Closed

Datetime format #39

lafama opened this issue Jul 12, 2012 · 12 comments

Comments

@lafama
Copy link

lafama commented Jul 12, 2012

How does one control the datetime format of a property. I have tried the following

[TypeConverter(typeof(CustomDateTimeTypeConverter))]
public DateTime? Dob
{
get { return _dob; }
set { _dob = value; }
}

public class CustomDateTimeTypeConverter : DateTimeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ((DateTime)value).ToString("dd-MM-yyyy");
}
return base.ConvertTo(context, CultureInfo.InvariantCulture, value, destinationType);
}
}

@JoshClose
Copy link
Owner

I will give you an example shortly here. Sorry for the slow reply.

Date formatting is probably a common enough thing to warrant a DateFormat property as a part of the configuration. I haven't got around to doing that yet.

@JoshClose
Copy link
Owner

What is wrong with what you were doing? That seemed to work fine for me.

@lafama
Copy link
Author

lafama commented Jul 27, 2012

Problem is this seems to work on my development machine but once i deploy it to the production server. It gives me errors

@JoshClose
Copy link
Owner

What are the errors you're seeing?

@JoshClose
Copy link
Owner

I'm closing this since I haven't heard back in 2 months.

@KevivL
Copy link

KevivL commented Mar 24, 2015

Do you have a config level / global type converter option or it needs to be attached on specific properties via type maps? Ex. When exporting, i want all dates written as yyyy-MM-dd. Is there a way to do that outside of on a per property basis in individual type maps?

@JoshClose
Copy link
Owner

Yes.

var options = new TypeConverterOptions
{
    Format = "yyy-MM-dd",
};
CsvHelper.TypeConversion.TypeConverterOptionsFactory.AddOptions<DateTime>(options);

@KevivL
Copy link

KevivL commented Mar 25, 2015

Actually... that looks to be global. So if in a process i'm reading and writing with differing standard formats, it's an issue right? I was hoping for something via the CsvConfiguration object? Since that's the object being passed to readers and writers. Is there a way to set that per reader / writer?

@JoshClose
Copy link
Owner

Not right now. It's planned though. #318

@TheCycoONE
Copy link

Took me hours to find this, could you put a TypeConverterOptionsFactory example on your page?

@SupremeMagus
Copy link

CsvHelper.TypeConversion.TypeConverterOptionsFactory

unable to find this class, using version 2.2.0

@JoshClose
Copy link
Owner

It was renamed to TypeConverterOptionsCache because it wasn't a factory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants