Calculating the Start and End date of a Week

Posted by Stephan | Filed under

I’ve had to calculate the start and end dates for the weeks of a specific year. See the sample below on how to achieve this in C# (at least in the WP7 framework).

   1: // General Idea:
   2: // We want to have some that in the the week we want to calculate 
   3: // the start and the end date for. For this, we are going to 
   4: // calculate the week of january 1st, add the number of weeks to 
   5: // this date so that we have a date in the target week. Now go 
   6: // back until we reach the first day of the corresponding
   7: // week and then add 6 days to get the last day in the week
   8:  
   9: // first, get the week number for January 1st for the 
  10: // specific year
  11: int jan1stWeekNumber = 
  12:     CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
  13:         new DateTime(Year, 1, 1), 
  14:         CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule, 
  15:         CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
  16:  
  17: int offset;
  18: // We need an offset to add the number of weeks to jan 1st 
  19: // because this date may not be in the first week of the year. 
  20: // Examples (we want a date in week 23):
  21: // jan 1st = week 1:               week 1 + week 23 = week 24
  22: // jan 1st = week 52 of last year: week 52 + week 23 = week 23
  23: // now differ between the following two cases:
  24: // 1. january 1st is in the last week of the previous year
  25: // 2. january 1st is in the first week of the same year
  26: if (jan1stWeekNumber == 1)
  27:     offset = 1;
  28: else
  29:     offset = 0;
  30:  
  31: // now add the week number including the offset to jan 1st
  32: DateTime dateInWeek = 
  33:     CultureInfo.CurrentCulture.Calendar.AddWeeks(
  34:         new DateTime(Year, 1, 1), 
  35:         WeekNr - offset);
  36:  
  37: // create dummy dates for first and last weekday
  38: DateTime firstDateInWeek = new DateTime(
  39:     dateInWeek.Year, 
  40:     dateInWeek.Month, 
  41:     dateInWeek.Day);
  42: DateTime lastDateInWeek;
  43: // go back from the current date until we get to the first day 
  44: // of the week, of course, depending on the current culture 
  45: // (in U.S. the first day is sunday, in europe, monday)
  46: while (firstDateInWeek.DayOfWeek != 
  47:     CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek)
  48: {
  49:     firstDateInWeek = firstDateInWeek.AddDays(-1);
  50: }
  51: // to get the last day of the week, just add 6 days to the 
  52: // start day
  53: lastDateInWeek = firstDateInWeek.AddDays(6);

DateTimePicker Control Combination

Posted by Stephan | Filed under

I’ve searched the net to find a way to have one control in WP7 Silverlight to select time and date at the same time. Although the WP7 Silverlight Toolkit contains a TimePicker and DatePicker control there is no combined one. So what I wanted is to bind one DateTime to TimePicker and DatePicker and automatically persist. Of course this won’t work because when I bind the DateTime with Mode=TwoWay, one change (either the date or the time) is overwritten. So I played around a bit and here is a short solution for those who require the same:

   1: <toolkit:TimePicker Value="{Binding To,Mode=TwoWay}" x:Name="myTimePicker" />
   2: <toolkit:DatePicker Value="{Binding ElementName=myTimePicker,Path=Value,Mode=TwoWay}" x:Name="myDatePicker" />

As you can see the TimePicker is bound to the DateTime object that contains time and date information and uses Mode=TwoWay. The DatePicker is now bound to the value of the TimePicker, also with Mode=TwoWay. With that, if the date is changed in the DatePicker it is also changed in the TimePicker as both use the same source.