Get Iso Week - CSharp System

CSharp examples for System:DateTime Week

Description

Get Iso Week

Demo Code


using System.Globalization;
using System.Text.RegularExpressions;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;//from   w w  w. j av  a  2 s . c  o  m

public class Main{
        #endregion
        public static string GetIsoWeek(DateTime date)
        {
            var calendar = CultureInfo.InvariantCulture.Calendar;

            if (date.DayOfWeek >= DayOfWeek.Monday && date.DayOfWeek <= DayOfWeek.Wednesday)
            {
                date = date.AddDays(3);
            }

            var week = calendar.GetWeekOfYear(
                date,
                CalendarWeekRule.FirstFourDayWeek,
                DayOfWeek.Monday);

            var year = date.Year;

            if (date.Month == 1 && week >= 52)
            {
                year -= 1;
            }

            return string.Format("{0}-W{1:00}", year, week);
        }
}

Related Tutorials