English 中文(简体)
日间检查
原标题:check sunday falls between two dates
  • 时间:2009-12-23 06:38:59
  •  标签:
  • c#
  • asp.net

如何确定太阳日为两个日期。 如果日复一日,从两天的差额中减去一天

最佳回答

如果我理解正确的话,你正在寻找这样的东西:

public static bool DoesIncludeSunday(DateTime startDate, DateTime endDate)
{
    bool r = false;
    TimeSpan testSpan = new TimeSpan(6, 0, 0, 0);
    TimeSpan actualSpan =endDate - startDate;

    if (actualSpan >= testSpan) { r = true; }
    else
    {
        DateTime checkDate = endDate;
        while (checkDate > startDate)
        {
            r = (checkDate.DayOfWeek == DayOfWeek.Sunday);
            if(r) { break; }
            checkDate = checkDate.AddDays(-1);
        }
    }

    return r;
}

最终结果必须是最近的日期。 第一部分简单地使我们无法检查开端和终点日期是否相隔了6天以上(这将包括一个日,因此无需继续)。 第二步只是从结束语检查(如果星期日在那里)开始一天后走。

一旦你知道太阳日是否是时段的一部分,你就可以对你从呼吁守则中想要的日期作任何改动。

问题回答

仅仅因为我要cl,我就这样写了:

public static int DaysExcludingSundays(DateTime start, DateTime end)
{
 return ((end - start).Days + 1) - ((((end - start).Days + 1) + (((int)start.DayOfWeek + 6) % 7)) / 7);
}

感到可以在不理解该守则的含义的情况下复制和复制该守则。 我感到迷惑。

缩略语:

int startOffset = ((int) start.DayOfWeek + 6) % 7;  
int totalInclusiveDays = (end - start).Days + 1;  
int numberOfSundays = (totalInclusiveDays + startOffset) / 7;  
int numberOfDaysWithoutSundays = totalInclusiveDays - numberOfSundays; 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签