In one of my applications I needed to display the range of a week knowing only the week number from a specific year. After I realize that a functionality like this is not provided out of the box I had to do some research.
I started with a DateTime extension that I founded at http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
Ok. This will help me to find the first day of a week for a given date. Next only to test I created a console application where given a date I found the week number and after that I print the week range.
At the beginning we have to establish the first day of the week. The reason is that in different cultures the week can start on Monday, Sunday or who knows what day.
public static DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
The rest of the code is simple. I added the data as string to simulate the reading from a config file. Here it is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace WeekRange
{
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
class Program
{
public static DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
public static int GetNumberOfWeekByDate(DateTime theTime)
{
CultureInfo ciGetNumber = CultureInfo.CurrentCulture;
int returnNumber = ciGetNumber.Calendar.GetWeekOfYear(theTime, CalendarWeekRule.FirstDay, firstDayOfWeek);
return returnNumber;
}
public static int GetNumberOfWeeksInYear(int theYear)
{
DateTime lastMoment = new DateTime(theYear, 12, 31);
return GetNumberOfWeekByDate(lastMoment);
}
public static void ShowShortPeriod(DateTime theDate, int yearWeeksNumber)
{
int fromNext = 0;
int weekNumber = GetNumberOfWeekByDate(theDate);
if (yearWeeksNumber == weekNumber)
{
DateTime firstDayOfNextYear = new DateTime(theDate.Year + 1, 1, 1);
if (firstDayOfNextYear.ToShortDateString() != firstDayOfNextYear.StartOfWeek(firstDayOfWeek).ToShortDateString())
{
weekNumber = 1;
fromNext = theDate.Year + 1;
}
else
{
fromNext = theDate.Year;
}
}
else
{
fromNext = theDate.Year;
}
DateTime fdt = theDate.StartOfWeek(firstDayOfWeek);
DateTime ldt = fdt.AddDays(6);
Console.Write("{2} is in week {0} from year {1}", weekNumber, fromNext, theDate.ToString("dd-MMM-yyyy"));
}
public static string GetWeekRange(int yearNr, int weekNr, int yearWeeksNumber)
{
DateTime firstDayOfYear = new DateTime(yearNr, 1, 1);
DateTime firstWeekStart = firstDayOfYear.StartOfWeek(firstDayOfWeek);
DateTime desiredWeekStartDate = firstWeekStart.AddDays(weekNr * 7 - 7);
DateTime desiredWeekEndDate = desiredWeekStartDate.AddDays(6);
return desiredWeekStartDate.ToString("dd-MMM-yyyy") + " to " + desiredWeekEndDate.ToString("dd-MMM-yyyy");
}
static void Main(string[] args)
{
string strCurrentDate = "2010/10/21";
string[] strCurrentDateArray = strCurrentDate.Split('/');
DateTime dt = new DateTime(Convert.ToInt32(strCurrentDateArray[0]),
Convert.ToInt32(strCurrentDateArray[1]),
Convert.ToInt32(strCurrentDateArray[2]));
// print week number for a specified date
ShowShortPeriod(dt, GetNumberOfWeeksInYear(dt.Year));
Console.WriteLine("");
//display the week range
int theYear = Convert.ToInt32(strCurrentDateArray[0]);
int theYearWeeksNumber = GetNumberOfWeeksInYear(theYear);
int weekNumber = GetNumberOfWeekByDate(dt);
Console.WriteLine("week range from {0}", GetWeekRange(theYear, weekNumber, theYearWeeksNumber));
Console.ReadKey();
}
}
}
The output of this is:
Hope this helps.