This short article will present a method to calculate a
WeekDiff method in C#.
We could first just look at the TimeSpan and divide by seven to get the amount of whole weeks two dates differs. Lets first implement it like this :
/// <summary>
/// Calculates the number of weeks between <paramref name="fromDate"/> and <paramref name="toDate"/>.
/// Also, the remainder days are looked upon and calculation will also see if weeks are different by adding fromDate with the remainding days
/// (i.e. the difference in total days minus N full weeks.
/// The weeks ago is the number of 'full weeks' plus an extra week if there are more days and the week numbers
/// are different between fromDate and fromDate+remainding days giving in some calculation N+1 weeks ago if the week numbers are different by adding the remainder.
/// </summary>
/// <param name="fromDate">The from date to calculate the number of weeks diff</param>
/// <param name="toDate">The to date to calculate the number of weeks diff</param>
/// <param name="startOfWeek">Start of week (DayOfWeek). Default will be Monday. For english culture, use Sunday.</param>
/// <param name="currentCulture">Specify culture, this will in turn use the Calendar of the culture for calculation. Fallbacks to CurrentCulture if not specified.</param>
/// <returns></returns>
public static int CalcWeekDiff(this DateTime fromDate, DateTime toDate)
{
if (fromDate > toDate)
{
var temp = fromDate;
fromDate = toDate;
toDate = temp;
}
if (currentCulture == null)
{
currentCulture = CultureInfo.CurrentCulture;
}
TimeSpan diff = toDate.Subtract(fromDate);
int weeks = diff.Days / 7;
return weeks;
}
But we could also look at the remainder part and include it also. Maybe two dates differs in 10 days, that is more than one week. If we qualify this as 'two weeks' we get:
/// <summary>
/// Calculates the number of weeks between <paramref name="fromDate"/> and <paramref name="toDate"/>.
/// Also, the remainder days are looked upon and calculation will also see if weeks are different by adding fromDate with the remainding days
/// (i.e. the difference in total days minus N full weeks.
/// The weeks ago is the number of 'full weeks' plus an extra week if there are more days and the week numbers
/// are different between fromDate and fromDate+remainding days giving in some calculation N+1 weeks ago if the week numbers are different by adding the remainder.
/// </summary>
/// <param name="fromDate">The from date to calculate the number of weeks diff</param>
/// <param name="toDate">The to date to calculate the number of weeks diff</param>
/// <param name="startOfWeek">Start of week (DayOfWeek). Default will be Monday. For english culture, use Sunday.</param>
/// <param name="currentCulture">Specify culture, this will in turn use the Calendar of the culture for calculation. Fallbacks to CurrentCulture if not specified.</param>
/// <returns></returns>
public static int CalcWeeksDiff(this DateTime fromDate, DateTime toDate)
{
if (fromDate > toDate)
{
var temp = fromDate;
fromDate = toDate;
toDate = temp;
}
if (currentCulture == null)
{
currentCulture = CultureInfo.CurrentCulture;
}
TimeSpan diff = toDate.Subtract(fromDate);
int weeks = diff.Days / 7;
int remainingDays = diff.Days % 7;
}
We might also look more into looking at the week number of the dates. Then we must also consider CultureInfo and CalendarWeekRule - lets choose FirstFullWeek and what is the start of week in the calender which differs also from country to country.
If we judge two dates then then to have different week numbers by adding the remainder part (i.e. diff.Days % 7) to give different week numbers, we add +1 to the final result. This should then work in special cases, such as years having 52 and 53 weeks around New Year.
The weeks diff calculation then looks like this:
/// <summary>
/// Calculates the number of weeks between <paramref name="fromDate"/> and <paramref name="toDate"/>.
/// Also, the remainder days are looked upon and calculation will also see if weeks are different by adding fromDate with the remainding days
/// (i.e. the difference in total days minus N full weeks.
/// The weeks ago is the number of 'full weeks' plus an extra week if there are more days and the week numbers
/// are different between fromDate and fromDate+remainding days giving in some calculation N+1 weeks ago if the week numbers are different by adding the remainder.
/// </summary>
/// <param name="fromDate">The from date to calculate the number of weeks diff</param>
/// <param name="toDate">The to date to calculate the number of weeks diff</param>
/// <param name="startOfWeek">Start of week (DayOfWeek). Default will be Monday. For english culture, use Sunday.</param>
/// <param name="currentCulture">Specify culture, this will in turn use the Calendar of the culture for calculation. Fallbacks to CurrentCulture if not specified.</param>
/// <returns></returns>
public static int CalcWeeksDiff(this DateTime fromDate, DateTime toDate, DayOfWeek startOfWeek = DayOfWeek.Monday, CultureInfo currentCulture = null)
{
if (fromDate > toDate)
{
var temp = fromDate;
fromDate = toDate;
toDate = temp;
}
if (currentCulture == null)
{
currentCulture = CultureInfo.CurrentCulture;
}
TimeSpan diff = toDate.Subtract(fromDate);
int weeks = diff.Days / 7;
int remainingDays = diff.Days % 7;
var cal = currentCulture.Calendar;
var fromDateWeekNo = cal.GetWeekOfYear(fromDate, CalendarWeekRule.FirstFullWeek, startOfWeek);
var fromDateWeekNoPlusRemDays = cal.GetWeekOfYear(fromDate.AddDays(remainingDays), CalendarWeekRule.FirstFullWeek, startOfWeek);
if (fromDateWeekNo != fromDateWeekNoPlusRemDays)
{
weeks++; //if week numbers are different, count +1 weeks (i.e. week diff does not have to be N 'full weeks', only that week numbers are different)
}
return weeks;
}
Then we can check if for example 28th of December 2022 and 2nd of January 2023 are one week between and they are.
var from = new DateTime(2022, 12, 28);
var to = new DateTime(2023, 1, 2);
var weeksDiff = from.CalcWeeksDiff(to);
weeksDiff.Dump("# of Weeks ago"); //Linqpad util method
So calculating weeks diff could take different approaches :
- Number of weeks differing meaning integral part of weeks : just do a TimeSpan (swap if to is before from date) and divide by 7
- Include considering if we have more than a integral part of weeks : consider also the remainder part - if remainder gives modulo % 7 > 0 we add +1 to the result
- We have seen that we should include more checking of the remainder part to consider if two dates have different week numbers. Then also include CalendarWeek rules, CultureInfo and DayOfWeek