0

I am using ASP:Calendar in ASP.NET Website. I need to retrieve DayofWeek whether 1stSunday,2nd Sunday,3rd Sunday or 4th Sunday similarly for other days of that month for the date selected. Kindly help in this. thanks in advance.

Chetan Goenka
  • 1,209
  • 2
  • 11
  • 17

3 Answers3

0

You probably already have DateTime variable. If yes, you can use this code to calculate week number:

var currentCulture = CultureInfo.CurrentCulture;
var weekNo = currentCulture.Calendar.GetWeekOfYear(
                new DateTime(2013, 12, 31), //Your date
                currentCulture.DateTimeFormat.CalendarWeekRule,
                currentCulture.DateTimeFormat.FirstDayOfWeek);

Good luck!

Nancy
  • 1
  • 3
0

Here is example from MSDN:

public class Example
{
   public static void Main()
   {
      DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
      DateTime date1 = new DateTime(2011, 1, 1);
      Calendar cal = dfi.Calendar;

      Console.WriteLine("{0:d}: Week {1} ({2})", date1, 
                        cal.GetWeekOfYear(date1, dfi.CalendarWeekRule, 
                                          dfi.FirstDayOfWeek),
                        cal.ToString().Substring(cal.ToString().LastIndexOf(".") + 1));       
   }
}

And also the link: http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx

0

I did the following in one of my projects to get current week number in a month

private static int GetWeekNum(DateTime date, CultureInfo culture) 
{ 
  var firstOfMonth = new DateTime(date.Year, date.Month, 1); 
  return GetWeekNumber(date, culture) - GetWeekNumber(firstOfMonth, culture) + 1; 
} 

private static int GetWeekNumber(DateTime date, CultureInfo culture) 
{ 
  return culture.Calendar.GetWeekOfYear(date, 
    culture.DateTimeFormat.CalendarWeekRule, 
    culture.DateTimeFormat.FirstDayOfWeek); 
}
HCJ
  • 529
  • 1
  • 6
  • 14