I need to get the date of the first and last day of the week knowing the week number.
I get a start date and an end date, representing the first and last day of a s
Subtract 1 year from your original DateTime and call your GetWeekNumber method again with that.
TimeSpan oneYear = dateTime.AddYears(1) - dateTime;
var lastYearWeekNumber = GetWeekNumber(dateTime.Subtract(oneYear));
This is my solution:
public static DateTime FirstDayOfWeek(DateTime date)
{
DayOfWeek fdow = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
int offset = fdow - date.DayOfWeek;
DateTime fdowDate = date.AddDays(offset);
return fdowDate;
}
public static DateTime LastDayOfWeek(DateTime date)
{
DateTime ldowDate = FirstDayOfWeek(date).AddDays(6);
return ldowDate;
}
The method above calculates first date wrong for 1st week of 2016.
Fixed code based on previous answers
public DateTime FirstDateOfWeek(
int year,
int weekOfYear)
{
var newYear = new DateTime(year, 1, 1);
var weekNumber = newYear.GetIso8601WeekOfYear();
DateTime firstWeekDate;
if (weekNumber != 1)
{
var dayNumber = (int) newYear.DayOfWeek;
firstWeekDate = newYear.AddDays(7 - dayNumber + 1);
}
else
{
var dayNumber = (int)newYear.DayOfWeek;
firstWeekDate = newYear.AddDays(-dayNumber + 1);
}
if (weekOfYear == 1)
{
return firstWeekDate;
}
return firstWeekDate.AddDays(7 * (weekOfYear - 1));
}
Plus test
[TestCase(2016, 1, 2016, 1, 4)]
[TestCase(2016, 2, 2016, 1, 11)]
[TestCase(2016, 52, 2016, 12, 26)]
[TestCase(2014, 1, 2013, 12, 30)]
public void FirstDateOfWeek_ShouldReturnCorrectDay(int year, int weekNumber, int correctYear, int correctMonth, int correctDay)
{
var helper = new DateHelper();
var result = helper.FirstDateOfWeek(year, weekNumber);
Assert.That(result.Year, Is.EqualTo(correctYear));
Assert.That(result.Month, Is.EqualTo(correctMonth));
Assert.That(result.Day, Is.EqualTo(correctDay));
}
Tim Schmelter's method didn't work for Swedish (sv-SE) culture where weeks start on Mondays. This has been tested with en-US and sv-SE.
public static DateTime GetFirstDateOfWeek(int year, int weekOfYear, CultureInfo cultureInfo)
{
DateTime jan1 = new DateTime(year, 1, 1);
int daysOffset = (int)cultureInfo.DateTimeFormat.FirstDayOfWeek - (int)jan1.DayOfWeek;
DateTime firstWeekDay = jan1.AddDays(daysOffset);
int firstWeek = cultureInfo.Calendar.GetWeekOfYear(jan1, cultureInfo.DateTimeFormat.CalendarWeekRule, cultureInfo.DateTimeFormat.FirstDayOfWeek);
if (firstWeek == 1)
{
weekOfYear -= 1;
}
return firstWeekDay.AddDays(weekOfYear * 7);
}
private static DateTime FirstDateOfWeek(int year, int weekOfYear)
{
var firstDate = new DateTime(year, 1, 4);
//first thursday of the week defines the first week (https://en.wikipedia.org/wiki/ISO_8601)
//Wiki: the 4th of january is always in the first week
while (firstDate.DayOfWeek != DayOfWeek.Monday)
firstDate = firstDate.AddDays(-1);
return firstDate.AddDays((weekOfYear - 1)*7);
}
Easyer and more efficient solution!
Since there are 7 days per week, you will know that Week 10 is the same as days 71 - 77. You know the number of days per month (don't forget leap year). Construct the exact dates and then, you can get information about those days.