Click here to Skip to main content
15,068,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get start and end date of particular week.
I am having three inputs :
1. Year
2. Week number
3. Week start day (Configurable : decided by configuration in our website not by standard or c#).

I am having Enum as below as per my requirement(So i am using enum of c# where sunday takes value 0):
public enum EN50160DayOfWeek
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}

I want start and end date by using my enum and week start day from my system (whether week start from sunday or monday or tuesday and so on).

What I have tried:

Unable to create logic.I want to create generic function.
Posted
Updated 17-May-17 3:31am
Comments
Richard MacCutchan 17-May-17 4:12am
   
Use a DateTime object.

1 solution

The first issue you have is to define "week number". I'm going to assume you mean the ISO Week Number (ISO week date - Wikipedia[^]) which equates to CalendarWeekRule.FirstFourDayWeek. To see why this is important consider this code snippet, which lists the week that 2017-January-16 falls in, in the Gregorian calendar, for the 3 different options for counting weeks:
C#
var cc = new GregorianCalendar();
dt = new DateTime(2017,1,16);
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Tuesday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Tuesday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Tuesday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Wednesday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Wednesday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Wednesday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Thursday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Thursday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Thursday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Friday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Friday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Friday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Saturday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Saturday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Saturday));
Console.WriteLine("Week {0} {1} {2}", cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday), cc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday));
It gives the results
Week 3 3 4
Week 2 2 3
Week 2 2 3
Week 3 2 3
Week 3 2 3
Week 3 2 3
Week 3 3 3
Your next problem is your enum. Firstly it is not well formed, MS advise that enums should always have an entry with a value of 0. I really can't see why you don't just use the standard enum - after all you don't really care about the number, just the "name" of the day
[Serializable]
[ComVisible(true)]
public enum DayOfWeek
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
}
To use the standard DateTime functions you are going to have to convert your enum to the standard, which you can do like this:
var Week_start = EN50160DayOfWeek.Tuesday;
var realday = (int)Enum.Parse(typeof(DayOfWeek), Week_start.ToString());
The next step is to work out a date for the week number you require, based on your configurable start of week day. In the code below I've chosen a "brute-force" approach - start on the first day of the year and keep adding a day until you get to the week you want. That is obviously going to be the first day of that week and adding 6 days will give you the date of the last day in that week:
C#
var year = 2017;
var week_number = 3;
var Week_start = EN50160DayOfWeek.Tuesday;

var realday = Enum.Parse(typeof(DayOfWeek), Week_start.ToString());

var cc = new GregorianCalendar();
var dt = new DateTime(year, 1, 1);
for (var i = 0; i < 365; i++)
{
    dt = dt.AddDays(1);
    if (week_number == cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, (DayOfWeek)realday)) break;
}

var startOfWeek = dt;
var endOfWeek = startOfWeek.AddDays(6);
If you think that method is too inefficient for weeks later in the year, then you can increment the date of YYYY-Jan-01 by 1 until you get to the start of Week 1. This isn't as silly as it sounds as it is only CalendarWeekRule.FirstDay that will guarantee Week 1 starts on 1st January. Once you have the start date of week 1 just add the appropriate number of days to find the week you want. Like this:
C#
var year = 2017;
var week_number = 3;
var Week_start = EN50160DayOfWeek.Tuesday;

var realday = Enum.Parse(typeof(DayOfWeek), Week_start.ToString());

var cc = new GregorianCalendar();
var dt = new DateTime(year, 1, 1);
while (cc.GetWeekOfYear(dt, CalendarWeekRule.FirstFourDayWeek, (DayOfWeek)realday) > 1)
{
    // Handle start of January being week 52 or 53 of the previous year (2017 for example)
    dt = dt.AddDays(1);
}
var startOfWeek = dt.AddDays((7 * (week_number - 1)));
var endOfWeek = startOfWeek.AddDays(6);
   
Comments
jekin77 17-May-17 11:56am
   
Perfectly explained

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900