How do you calculate the amount of weeks between 2 ISO8601 dates using only the week and year number?
Input: year1 week1 year2 week2
Output: Amount of weeks according to ISO8601
I can calculate the amount of weeks in a year:
public static int AmountOfWeeksInYearIso8601(this DateTime dateTime)
{
var year = dateTime.Year;
var g = Math.Floor((year - 100d) / 400d) - Math.Floor((year - 102d) / 400d);
var h = Math.Floor((year - 200d) / 400d) - Math.Floor((year - 199d) / 400d);
var f = 5 * year + 12 - 4 * (Math.Floor(year / 100d) - Math.Floor(year / 400d)) + g + h;
return f % 28 < 5 ? 53 : 52;
}
Create
DateTime
values corresponding to the Mondays of the two weeks. See Calculate date from week number for how to do this. Subtract these to get the difference as aTimeSpan
. Request itsDays
property to get the difference as a number of days. Divide by 7 to get the difference as a number of weeks.