Click here to Skip to main content
15,072,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to get the week number of the month in the label based on the date selected in the asp.net calender control. I dont want week numbers by yearly wise, I want monthly wise. for example this month contails six weeks so , I want weeks from 1 to 6 to be displayed based on the selected date.
Posted
Updated 4-Oct-11 4:05am
v3

Try this one (an extension method)

C#
/// <summary>
/// Gets the week number for a provided date time value based on the current culture settings.
/// </summary>
/// <param name="dateTime">The date time.</param>
/// <returns>The week number</returns>
public static int GetWeekOfYear(this DateTime dateTime) {
    var culture = CultureInfo.CurrentUICulture;
    var calendar = culture.Calendar;
    var dateTimeFormat = culture.DateTimeFormat;

    return calendar.GetWeekOfYear(dateTime, dateTimeFormat.CalendarWeekRule, dateTimeFormat.FirstDayOfWeek);
}



As your question is not clear, if any, check this link out.

http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx[^]

Cheers
   
Comments
Dalek Dave 4-Oct-11 4:36am
   
Nice
Try this

C#
public static int WeekDay(DateTime dt) 
        { 
            // Set Year 
            int yyyy = dt.Year; 
            // Set Month 
            int mm = dt.Month; 
 
            // Set Day 
            int dd = dt.Day; 
            // Declare other required variables 
            int DayOfYearNumber; 
            int Jan1WeekDay; 
            int WeekDay; 
 
            int i, j, k, l; 
            int[] Mnth = new int[12] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 
 
            // Set DayofYear Number for yyyy mm dd 
            DayOfYearNumber = dd + Mnth[mm - 1]; 
            // Increase of Dayof Year Number by 1, if year is leapyear and month is february 
            if ((IsLeapYear(yyyy) == true) && (mm == 2)) 
                DayOfYearNumber += 1; 
            // Find the Jan1WeekDay for year 
            i = (yyyy - 1) % 100; 
            j = (yyyy - 1) - i; 
            k = i + i / 4; 
            Jan1WeekDay = 1 + (((((j / 100) % 4) * 5) + k) % 7); 
            // Calcuate the WeekDay for the given date 
            l = DayOfYearNumber + (Jan1WeekDay - 1); 
            WeekDay = 1 + ((l - 1) % 7); 
            return WeekDay; 
        } 
 
        public static bool IsLeapYear(int yyyy) 
        { 
 
            if ((yyyy % 4 == 0 && yyyy % 100 != 0) || (yyyy % 400 == 0)) 
                return true; 
            else 
                return false; 
        }  


http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/bf504bba-85cb-492d-a8f7-4ccabdf882cb[^]
   
v2
   
v2
try this...

Dim weekNum as Integer = DatePart(DateInterval.WeekOfYear, Date.Now)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900