DateTime Range
-
I have 2 DateTime Vars. DateTime dtFrom = '1/1/2005'; DateTime dtTo = '1/24/2005'; How can I select a period of: Week, 2 Weeks, Month... And Divide the date range into it to something like this: Example: 1 week: 1 2 3 4 5 ======== ======== ========= ========= ========= 1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005 1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005 ======== ======== ========= ========= =========
-
I have 2 DateTime Vars. DateTime dtFrom = '1/1/2005'; DateTime dtTo = '1/24/2005'; How can I select a period of: Week, 2 Weeks, Month... And Divide the date range into it to something like this: Example: 1 week: 1 2 3 4 5 ======== ======== ========= ========= ========= 1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005 1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005 ======== ======== ========= ========= =========
First Row Differences:
1-1-05, 1-2-05 = 1 Day
1-2-05, 1-9-05 = 7 Days
1-9-05, 1-16-05 = 7 Days
1-16-05, 1-23-05 = 7 DaysSecond Row Differences:
1-1-05, 1-8-05 = 7 Days
1-8-05, 1-15-05 = 7 Days
1-15-05, 1-22-05 = 7 Days
1-22-05, 1-24-05 = 2 DaysI don't see any patterns in the differences here, so what exactly do you want again? What do you mean by "divide the date range into it" ? What do you want to do? Are looking for every Sunday/Saturday in the month? The work-week days? Display a calender to the screen? David
-
First Row Differences:
1-1-05, 1-2-05 = 1 Day
1-2-05, 1-9-05 = 7 Days
1-9-05, 1-16-05 = 7 Days
1-16-05, 1-23-05 = 7 DaysSecond Row Differences:
1-1-05, 1-8-05 = 7 Days
1-8-05, 1-15-05 = 7 Days
1-15-05, 1-22-05 = 7 Days
1-22-05, 1-24-05 = 2 DaysI don't see any patterns in the differences here, so what exactly do you want again? What do you mean by "divide the date range into it" ? What do you want to do? Are looking for every Sunday/Saturday in the month? The work-week days? Display a calender to the screen? David
you were reading this wrong. 1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week) 1/2/2005 - 1/8/2005 - 2 (full second week) ...and so on... got it?
-
you were reading this wrong. 1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week) 1/2/2005 - 1/8/2005 - 2 (full second week) ...and so on... got it?
Ok, well you could try something like:
DateTime dtStart = new DateTime(2005, 1, 1);
DateTime dtEnd = new DateTime(2005, 1, 30);
DateTime dtCurrent = dtStart;while (dtCurrent < dtEnd)
{
double dDays;
if (dtCurrent.DayOfWeek != DayOfWeek.Sunday)
dDays = 6.0 - (double)dtCurrent.DayOfWeek;
else if ((dtCurrent.Day + 6) <= dtEnd.Day)
dDays = 6.0;
else
dDays = dtEnd.Day - dtCurrent.Day;Console.Write(dtCurrent + " - "); DateTime dtEndOfWeek = dtCurrent.AddDays(dDays); Console.WriteLine(dtEndOfWeek); dtCurrent = dtEndOfWeek.AddDays(1.0);
}
David