Get Week Number - CSharp System

CSharp examples for System:DateTime Week

Description

Get Week Number

Demo Code


using System.Threading.Tasks;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from   w w w .j  a  va  2s .  co m

public class Main{
        public static int GetWeekNumber(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
            return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        }
}

Related Tutorials