First Date Of Week ISO - CSharp System

CSharp examples for System:DateTime Week

Description

First Date Of Week ISO

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;// ww  w  .ja v a2  s  .  c o  m

public class Main{
        public static DateTime FirstDateOfWeekISO8601(int year, int weekOfYear)
        {
            DateTime jan1 = new DateTime(year, 1, 1);
            int daysOffset = DayOfWeek.Thursday - jan1.DayOfWeek;

            DateTime firstThursday = jan1.AddDays(daysOffset);
            var cal = CultureInfo.CurrentCulture.Calendar;
            int firstWeek = cal.GetWeekOfYear(firstThursday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            var weekNum = weekOfYear;
            if (firstWeek <= 1)
            {
                weekNum -= 1;
            }
            var result = firstThursday.AddDays(weekNum * 7);
            return result.AddDays(-3);
        }
}

Related Tutorials