我在ASP.NET网站上使用ASP:Calendar。我需要检索DayofWeek,无论是1stSunday,第2个星期日,第3个星期天还是第4个星期日,对于所选日期,该月的其他日期类似。请帮忙。提前谢谢。
答案 0 :(得分:0)
您可能已经拥有DateTime变量。如果是,您可以使用此代码计算周数:
var currentCulture = CultureInfo.CurrentCulture;
var weekNo = currentCulture.Calendar.GetWeekOfYear(
new DateTime(2013, 12, 31), //Your date
currentCulture.DateTimeFormat.CalendarWeekRule,
currentCulture.DateTimeFormat.FirstDayOfWeek);
祝你好运!
答案 1 :(得分:0)
以下是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));
}
}
还有链接:http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx
答案 2 :(得分:0)
我在其中一个项目中执行了以下操作,以获取一个月内的当前周数
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);
}