0

I am trying to request data from a Webserver. For that I need a list of dates represented as strings in YYYY-MM-DD format. I have seen these three posts one, two and three but none of them worked for me. I have tried this and the other posts solutions too:

DateTime startDate = new DateTime(2018, 9, 7);
DateTime endDate = new DateTime(2018, 9, 30);

foreach (DateTime day in AllDays(startDate, endDate))
                {
                    string dayX = day.ToString("yyyy-mm-dd", CultureInfo.InvariantCulture);
                    InsertEntry(dayX, requestLimit, connection);

                }

Using the following Method:

 private static IEnumerable<DateTime> AllDays(DateTime from, DateTime to)
        {
            for (DateTime day = from.Date; day.Date <= to.Date; day = day.AddDays(1))
                yield return day;
        }

Unfortunately the Date that I recieve as a String after converting the DateTime Object using the first code snippet, looks like this : 2018-00-07 which obviously is not a valid date. I seem to be unable to find my mistake. Therefore my questions are:

1) Is my conversion wrong or is the error part of the Enumerable creation?

2) How is this (efficiently) done? Overhead is kind of important since this will be running on a server.

Thewickedislick
  • 305
  • 1
  • 4
  • 14

0 Answers0