gpt4 book ai didi

c# - 计算给定日期范围内星期一的数量

转载 作者:IT王子 更新时间:2023-10-29 04:28:13 34 4
gpt4 key购买 nike

给定一个日期范围,我需要知道该范围内有多少个星期一(或星期二、星期三等)。

我目前正在使用 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;
}

关于c# - 计算给定日期范围内星期一的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/248273/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com