loop between two dates c#

for (DateTime date = Idate.Date; date.Date < FDate.Date; date = date.AddDays(1)) { //lógica aquí..... }

2 Responses

Date logic, at times, can be expensive.

May I recommend getting the difference in the dates using TimeSpan, then performing similar loop action such as:

for (i = 0; i < timespan.TotalDays; i++)
@s c Thnks S C , you're right, is much better, i got the difference between my Initial Date and Final Date and use the timespan. Example:

TimeSpan diff = FDate.Date - IDate.Date;
for(int i = 0; i < diff.TotalDays; i++)
{
DateTime myDate = i > 0 ? IDate.AddDays(i) : IDate;
}

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.