## Firt day and last day of week (weeknumber as parameter ans year)

### Firt day and last day of week (weeknumber as parameter ans year)

Hi,

I want ot make functions like this:

int getFirtDayOfWeek(int weeknumber, int year);
int getLastDayOfWeek(int weeknumber, int year);

Firts I calculate the count of weeks like this:

DateTime lastDay = new DateTime(DateTime.Now.Year, 12, 31);
DayOfWeek dayOfWeek = lastDay.DayOfWeek;
CultureInfo myCI = new CultureInfo("nl-BE");
Calendar myCal = myCI.Calendar;
CalendarWeekRule myCWR =
myCI.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCI.DateTimeFormat.FirstDayOfWeek;
int weeks = myCal.GetWeekOfYear(lastDay, myCWR,
myFirstDOW);

So for this year there are 53 weeks.
Now I want the firts day of week 2 for example ( = 7)
and the last day of week 2 (= 13)
How can I do that?

### Firt day and last day of week (weeknumber as parameter ans year)

Hi,

This is my (untested) guess:

int getFirtDayOfWeek(int weeknumber, int year) {
System.DateTime date = new DateTime(year, 1, 1);
return date.Day;
}
int getLastDayOfWeek(int weeknumber, int year){
System.DateTime date = new DateTime(year, 1, 1);
date.AddDays((7 * weeknumber) + 7);
return date.Day;
}

I guess you'd need to add checks for the last week of the year...

-Jeroen

### Firt day and last day of week (weeknumber as parameter ans year)

I will assume you want to use ISO 8601 standard for
week numbers (the european way).

Not really. 2009 has a week 53, but not 2007 or 2008.

See code below for one way of doing it.

Arne

================================================

using System;

namespace E
{
public class Program
{
public static int WeekNumber(int year, int mon, int day)
{
int a = (14 - mon) / 12;
int y = year + 4800 - a;
int m = mon + 12*a - 3;
int JD = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 +
y/400 - 32045;
int d4 = (((JD + 31741 - JD % 7) % 146097) % 36524) % 1461;
int L = d4 / 1460;
int d1 = ((d4 - L) % 365) + L;
return d1 / 7 + 1;
}
public static DateTime GetFirstDayOfWeek(int weeknumber, int year)
{
DateTime res = new DateTime(year, 1, 1);
res = res.AddDays((weeknumber - 1) * 7 - 4);
while(WeekNumber(res.Year, res.Month, res.Day) != weeknumber
|| res.DayOfWeek != DayOfWeek.Monday)
{
}
return res;
}
public static DateTime GetLastDayOfWeek(int weeknumber, int year)
{
DateTime res = new DateTime(year, 1, 1);
res = res.AddDays((weeknumber - 1) * 7 + 3);
while(WeekNumber(res.Year, res.Month, res.Day) != weeknumber
|| res.DayOfWeek != DayOfWeek.Sunday)
{
}
return res;
}
public static void Main(string[] args)
{
Console.WriteLine(GetFirstDayOfWeek(2, 2008));
Console.WriteLine(GetLastDayOfWeek(2, 2008));
Console.WriteLine(GetFirstDayOfWeek(53, 2009));
Console.WriteLine(GetLastDayOfWeek(53, 2009));
}
}
}

Thanks a lot!!!