如何计算一周内的天数?例如,2015年1月的第一周有4天,因此该功能将返回4 .....用户从日期选择器中选择月份,因此可能是任何一年
找到了类似问题的链接,但无法找到该页面
答案 0 :(得分:1)
三种情况:
答案 1 :(得分:1)
试试这个:
/// <summary> Get days in a week </summary>
/// <param name="year">Number of year</param>
/// <param name="month">Number of month. From 1 to 12</param>
/// <param name="week">Number of week. From 1 to 5</param>
/// <returns>Count of days</returns>
public static int DaysInWeek(int year, int month, int week)
{
var weekCounter = 1;
var daysCounter = 0;
var dayInMonth = DateTime.DaysInMonth(year, month);
for (var i = 1; i <= dayInMonth; i++)
{
++daysCounter;
var date = new DateTime(year, month, i);
if (date.DayOfWeek == DayOfWeek.Sunday || i == dayInMonth)
{
if (weekCounter == week)
return daysCounter;
weekCounter++;
daysCounter = 0;
}
}
return 7;
}