I've had an attempt at the code you might need to achieve your task. it will (for a given month and year) itterate over the days and output the totals for that week to the screen, all you need do is impliment the "GetTotalForDay(DateTime CurrentDay)" function and make the output more attractive.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Testing
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int Year = 2007;
int Month = 12;
//Create a Totals array one Int node for each full week
ArrayList Totals = new ArrayList(FullWeeksInMonth(Year, Month));
for (int i = 0; i < FullWeeksInMonth(Year, Month); i++)
{
Totals.Add(new int());
}
//Itterate over each days of the full weeks
for (int i = FirstMonday(Year, Month); i <= LastSunday(Year, Month); i++)
{
DateTime CurrentDay = new DateTime(Year, Month, i);
int CurrentWeekNumber = WeekNumber(CurrentDay);
int CurrentWeekTotal = (int)Totals[CurrentWeekNumber-1];
CurrentWeekTotal += GetTotalForDay(CurrentDay);
Totals[CurrentWeekNumber - 1] = CurrentWeekTotal;
}
//now we write out the data of our Totals List
for (int i = 0; i < Totals.Count; i++)
{
Response.Write(string.Format("Week {0} Total: {1}<br/>", i + 1, Totals[i]));
}
}
private int GetTotalForDay(DateTime CurrentDay)
{
int outValue = 0;
//Get Total for CurrentDay
return outValue;
}
private int FirstMonday(int Year, int Month)
{
DateTime FirstMonday = new DateTime(Year, Month, 1);
while (!(FirstMonday.DayOfWeek == DayOfWeek.Monday))
{
FirstMonday = FirstMonday.AddDays(1);
}
return FirstMonday.Day;
}
private int LastSunday(int Year, int Month)
{
int LastDay = DateTime.DaysInMonth(Year, Month);
DateTime LastSunday = new DateTime(Year, Month, LastDay);
while (!(LastSunday.DayOfWeek == DayOfWeek.Sunday))
{
LastSunday = LastSunday.AddDays(-1);
}
return LastSunday.Day;
}
private int WeekNumber(DateTime theDate)
{
int DaysAfterFirstMonday = theDate.Day - FirstMonday(theDate.Year,theDate.Month);
return (DaysAfterFirstMonday / 7)+1;
}
private int FullWeeksInMonth(int Year, int Month)
{
return ((LastSunday(Year, Month) - FirstMonday(Year, Month)) / 7) + 1;
}
}
}
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88:





by: jasondavidcarrPosted on 2007-12-08 at 16:31:35ID: 20435654
.NET handles most of this for you. Below is some code that should demonstrate how to get the first and last day of the first full week in the month specified. If you need more help, let me know.
Select allOpen in new window