SlopTonio
SlopTonio

Reputation: 1105

c# foreach to loop through all dates between two datepickers

I have two datepickers

string dateFIRST = (dateSTART.Value.ToString("yyMMdd"));
string dateLAST = (dateEND.Value.ToString("yyMMdd"));

How can I use a foreach loop to iterate through all dates using the "yymmdd" format? Also, Can we possibly store the "yyMMdd" into an array since it will change each time during the foreach loop?

Upvotes: 0

Views: 2515

Answers (2)

Nininea
Nininea

Reputation: 2739

List<string> result = new List<string>();
        for (DateTime d = dateSTART.Value; d <= dateEND.Value; d = d.AddDays(1))
        {
            result.Add(d.ToString("yyMMdd"));
        }

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67273

For starters, you need to get the dates in DateTime format, not as strings.

for (DateTime d = dateSTART.Value; d <= dateEND.Value; d = d.AddDays(1))
{
    // d contains the date for this iteration
}

Upvotes: 9

Related Questions