1

I am looking to get count of weeks in current year for ISO 8601. I was reading it also depends on location and calendar. I am intrested for ISO8601. I couldn't found any solution. The similar thing i found was il_guru's which counts week number based on date. How could i nevertheless get count of s weeks for ISO8601?

This is @il_guru's code below: Source link

// This presumes that weeks start with Monday.
// Week 1 is the 1st week of the year with a Thursday in it.
public static int GetIso8601WeekOfYear(DateTime time)
{
    // Seriously cheat.  If its Monday, Tuesday or Wednesday, then it'll 
    // be the same week# as whatever Thursday, Friday or Saturday are,
    // and we always get those right
    DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
    if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
    {
        time = time.AddDays(3);
    }

    // Return the week of our adjusted day
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Arie
  • 3,041
  • 7
  • 32
  • 63
  • I suggest using [Noda Time](https://nodatime.org/3.0.x/userguide/weekyears) instead of trying to implement this yourself, specifically _WeekYearRules.Iso, which implements the rule specified in ISO-8601_ – stuartd Jul 26 '20 at 20:46
  • https://en.wikipedia.org/wiki/ISO_week_date may help you: you have 4 different way to calculate it (all are equivalent, just one may be better for you, depending on how you have the dates) – Giacomo Catenazzi Jul 27 '20 at 08:05

0 Answers0