计算一个给定日期范围星期一的数 [英] Count number of Mondays in a given date range
本文介绍了计算一个给定日期范围星期一的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个日期范围,我需要知道很多星期一(或星期二,星期三等),如何在这个范围内。
Given a date range, I need to know how many Mondays (or Tuesdays, Wednesdays, etc) are in that range.
我目前工作在C#。
推荐答案
试试这个:
static int CountDays(DayOfWeek day, DateTime start, DateTime end)
{
TimeSpan ts = end - start; // Total duration
int count = (int)Math.Floor(ts.TotalDays / 7); // Number of whole weeks
int remainder = (int)(ts.TotalDays % 7); // Number of remaining days
int sinceLastDay = (int)(end.DayOfWeek - day); // Number of days since last [day]
if (sinceLastDay < 0) sinceLastDay += 7; // Adjust for negative days since last [day]
// If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
if (remainder >= sinceLastDay) count++;
return count;
}
这篇关于计算一个给定日期范围星期一的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文