如何计算两个日期之间的星期日没有 [英] How to calculate no of sundays between two dates
本文介绍了如何计算两个日期之间的星期日没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想计算两个日期之间的星期日。
请让我知道如何计算它?
感谢你
Mohammad wasif
Hi All,
I want to calculate no of Sunday between two dates.
Please let me know how to calculate it?
Thanking you
Mohammad wasif
推荐答案
请参阅下面的代码,前提是sDate< fDate。
添加条件来操纵sDate(startDate)和fDate(finishDate)
See the code below, provided that sDate < fDate.
Add conditions to manipulate the sDate(startDate) and fDate(finishDate)
//DateTime fdate = DateTime.Now.AddDays(10);
DateTime fdate = yourFinishDate;
//DateTime sdate = DateTime.Now.AddDays(-1);
DateTime sdate = yourStartDate;
TimeSpan ts = fdate - sdate;
var sundays = ((ts.TotalDays / 7) + (sdate.DayOfWeek == DayOfWeek.Sunday || fdate.DayOfWeek == DayOfWeek.Sunday || fdate.DayOfWeek > sdate.DayOfWeek ? 1 : 0));
sundays = Math.Round(sundays - .5, MidpointRounding.AwayFromZero);
Btw,代码在c#中。祝你好运!
Btw, code in c#. Good luck!
DateTime fdate = yourFinishDate;
DateTime sdate = yourStartDate;
int CountSundays = (1 + fdate.Subtract(sdate).Days + (6 + (int)sdate.DayOfWeek) % 7) / 7;
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;
}
[source]
-KR
[source]
-KR
这篇关于如何计算两个日期之间的星期日没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文