-1

In this code item.date_time looks like "2017-10-18T04:57:39.000Z". When I put this (or any other similar string) instead of item.date_time - it works well. But despite the fact that item.date_time is equal to strings like this - it calls System.FormatException when I use it.

static void Main(string[] args)
{
    eventList = new List<CsvFile>();
    StreamReader sr = new StreamReader("logs.csv");
    string data = sr.ReadLine();

    while (data != null)
    {
        string[] line = data.Split(',');
        ReadString(line);
        data = sr.ReadLine();
    }
} 

class CsvFile
{
    public String date_time;
    public DateTime datetime;
}

static void ReadString(String[] str)
    {
        CsvFile item = new CsvFile();
        item.date_time = str[0];
        item.datetime = DateTime.ParseExact(item.date_time, "yyyy-MM-ddTHH:mm:ss.000Z", CultureInfo.InvariantCulture);
        eventList.Add(item);
    }

I went through dozens of questions and answers about datetime issues today, but found nothing can solve this strange thing. Any ideas what is the problem with my code?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 3
    There is obviously a value that does not conform to your expected DateTime format pattern. Use the debugger to figure out what it is. Maybe there is an empty line or you have spaces or the encoding is different or there is a hidden character.... The only one that can figure this out is you, you have access to the values where it fails and we don't (*because you did not include them in your question*) – Igor Oct 19 '17 at 20:43
  • Are you absolutely sure the strings are the same? Does `item.date_time` have quotes or escaped characters? Print them both to the console. – Blorgbeard Oct 19 '17 at 20:43
  • 2
    Or use `DateTime.TryParseExact`, and if it returns false, print *just that* value to the console. – Jon Skeet Oct 19 '17 at 20:44
  • @Blorgbeard I took example with "2017-10-18T04:57:39.000Z" directly from Console.Write(item.date_time); Also in Debugger str[0] is equal "\"2017-10-18T04:57:39.000Z\"" – twin_choke Oct 19 '17 at 20:54

1 Answers1

2

str[0] is equal "\"2017-10-18T04:57:39.000Z\""

Your string has quotes around it (hence the \" indicators). Trim those before parsing:

    item.date_time = str[0].Trim('"');
    item.datetime = DateTime.ParseExact(item.date_time, "yyyy-MM-ddTHH:mm:ss.000Z", CultureInfo.InvariantCulture);

You might consider using TryParseExact so you can determine if the parse was successful and show a better error message (like which record you're on, what the input value is, etc.) rather than throwing an exception.

Another alternative is to use a CSV parsing library that can handle both the quotes around values and the date/time parsing for you.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you so much, @D Stanley, that works perfect for me. I am absolute newbie in coding and of course I know it wasn't a smart question. Anyway I am thankful to everyone who hepled, especially you. – twin_choke Oct 19 '17 at 21:08
  • @twin_choke Don't be afraid to ask - everyone starts somewhere. – D Stanley Oct 19 '17 at 21:35