9

I am working with CsvHelper and being able to parse csv file. My question is how can I parse the Date into DateTime object

I want to convert it via CsvHelper while it is parsing the csv rather than iterating the collection

public static List<StockModel> SplitCsv(string csv) { var textReader = new StringReader(csv); var csvr = new CsvReader(textReader); csvr.Configuration.RegisterClassMap<ModelMap>(); var records = csvr.GetRecords<StockModel>().ToList(); return records; } public class StockModel { public string Date { get; set; } // I want this object to be DateTime public string Base { get; set; } public string Open { get; set; } } public sealed class ModelMap : CsvClassMap<StockModel> { public ModelMap() { Map(m => m.Date); Map(m => m.Base); Map(m => m.Open); } } 

CSV example

Date,Base,Open 2016-02-29,1437.530029,1445.839966 2016-02-25,1431.439941,1431.439941 2016-02-24,1430.459961,1432.430054 
4
  • What did you try? what are base and open supposed to be? Commented Mar 14, 2016 at 14:46
  • @Valentin, I want to convert it via CsvHelper Commented Mar 14, 2016 at 14:50
  • Does it make sense stackoverflow.com/questions/31817473/… ? Commented Mar 14, 2016 at 15:06
  • 1
    You might explain how it is not working - it converts from any format legal for the current culture Commented Mar 14, 2016 at 19:12

3 Answers 3

4

From my understanding of CsvHelper, The default built in converters will handle most cases of type conversion where it should be able to convert the type of the properties of your class. No need to make them all strings. Just put them in the type you want. Once the property name matches the column name (if present) in the csv then it auto maps those fields to their matching property

public class StockModel { //2016-02-29 public DateTime Date { get; set; } // CsvHelper should be able to infer type //1437.530029 public decimal Base { get; set; } //1445.839966 public decimal Open { get; set; } } public static List<StockModel> SplitCsv(string csv) { var textReader = new StringReader(csv); var csvr = new CsvReader(textReader); var records = csvr.GetRecords<StockModel>().ToList(); return records; } 

From Wiki on github

Using CsvHelper is really easy. It's default settings are setup for the most common scenarios.

Here is a little setup data.

Actors.csv:

Id,FirstName,LastName 1,Arnold,Schwarzenegger 2,Matt,Damon 3,Christian,Bale 

Actor.cs (custom class object that represents an actor):

public class Actor { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } 

Reading

Reading the CSV file using CsvReader:

var csv = new CsvReader( new StreamReader( "Actors.csv" ) ); var actorsList = csv.GetRecords<Actor>(); 
Sign up to request clarification or add additional context in comments.

Comments

3

This can be achieved by registering a ClassMap:

using var reader = new StreamReader(@""); using var csv = new CsvReader(reader, CultureInfo.InvariantCulture); csv.Context.RegisterClassMap<TransactionLineMap>(); // <-- this line var records = csv.GetRecords<TransactionLine>(); 

In the ClassMap, you can define the format of the DateTime using TypeConverterOption.Format("your-date-format")

internal class TransactionLineMap : ClassMap<TransactionLine> { public TransactionLineMap() { Map(m => m.Timestamp) .TypeConverter<CsvHelper.TypeConversion.DateTimeConverter>() .TypeConverterOption.Format("yyyy-MM-dd"); } } 

1 Comment

Does it support a custom converter? Like converting epoch time to DateTime?
-1

May be this will help you:

 string dateTime = "2016-02-29"; DateTime dt; DateTime.TryParseExact(dateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt); 

put proper code from this example into your set operator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.