简体   繁体   English

在Ajax日历扩展程序控件中禁用周末和国定假日

[英]Disabling weekend and national Holidays in ajax calendar extender control

I have been trying to use an ajax CalenderExtender for my application. 我一直在尝试为我的应用程序使用ajax CalenderExtender。

I have many small operations in my app like adding duration to start date of a task to find the finish date, change duration of a task if its end date is changed (depending on its start date),etc. 我的应用程序中有很多小操作,例如将持续时间添加到任务的开始日期以查找完成日期,如果任务的结束日期已更改(取决于其开始日期),则更改任务的持续时间等。

But while I do all these operations I want to skip all the holidays and saturday , sundays from the calculations for eg. 但是,当我执行所有这些操作时,我想从例如的计算中跳过所有的假期和周六,周日。 a task starting on 01/23/2014 with a duration of 5 days should finish on 01/29/2014 (adding 2 days for sat n sun in duration) instead of 01/27/2014. 从2014年1月23日开始,持续时间为5天的任务应在2014年1月29日完成(持续时间为连续2天增加2天),而不是2014年1月27日。 Same should be performed on other operations as well. 其他操作也应执行相同的操作。

Is there a way to do this? 有没有办法做到这一点?

For Sundays and Saturdays, it's easy. 对于周日和周六,这很容易。 Just check the DateTime.DayOfWeek property of your dates. 只需检查DateTime.DayOfWeek属性即可。

If you have an operation that will start on date start and will end on date end , you can see what dates are Saturdays or Sundays like this: 如果您有一项将在日期start并在日期end ,则可以看到如下所示的日期是星期六或星期日:

List<DateTime> satsAndSundays;
for (DateTime temp = start; temp <= end; temp.AddDays(1))
{
    if (temp.DayOfWeek == DayOfWeek.Sunday ||
        temp.DayOfWeek == DayOfWeek.Saturday)
    {
        satsAndSundays.add(temp);
    }
}

And since you can know how many days there are between start and end by doing something like: 由于您可以执行以下操作来知道startend之间有多少天:

TimeSpan span = end - start;
int totalDays = (int)span.TotalDays;
// TotalDays is actually a double, I'm just discarding the non integer part.

You may fid out how many work days you have there by doing totalDays - satsAndSundays.Count . 您可以通过执行totalDays - satsAndSundays.CounttotalDays - satsAndSundays.Count您在那里有totalDays - satsAndSundays.Count

Edit: I just read the question again. 编辑:我只是再次阅读了问题。 If you want a task to start on a given date, and take x work days, you can do it like this: 如果您希望某项任务在给定的日期开始并花费x工作日,则可以这样执行:

DateTime end = start;
for (int i = x; i >= 0;) // the third parameter of the for is empty on purpose
{
    end = end.AddDays(1);
    if (end.DayOfWeek != DayOfWeek.Saturday &&
        end.DayOfWeek != DayOfWeek.Sunday)
    {
        i--;
    }
}

Afther the loop, end will be x workdays after start (provided there are no holidays in between). 在循环之后, end将是start后的x工作日(前提是之间没有假期)。

For holidays, though, there is no alghoritm for that in the framework. 不过,对于假期来说,框架中没有针对此的算法。 You need to fetch them from some source (a file, a database, a web service etc.). 您需要从某些来源(文件,数据库,Web服务等)中获取它们。 Or you could write your own program to figure them out - most holidays that are not on a fixed date do follow formulas when it comes to when they happen. 或者,您可以编写自己的程序来解决这些问题-大多数不在固定日期的假期的发生时间都遵循公式。 Do take into account, however, that holidays may vary by culture and region. 但是请务必考虑到,假期可能因文化和地区而异。 If your application is to be used throughout a country, for example, it may be quite the effort to implement city-wide holidays. 例如,如果要在一个国家/地区使用您的应用程序,则实施全市假期可能是相当大的努力。 Depending on your needs, it might even be better to either let the users input which days are holidays, or making your own database your app can access and use. 根据您的需求,最好让用户输入假期的日期,或者创建自己的数据库以供您的应用访问和使用。

Try this one.. 试试这个

private string GetDatesOfSundays(DateTime DatMonth)  
    {  
        string sReturn = "";  
        int iDayOffset = DatMonth.Day - 1;  
        DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));  
        DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));  
        while (DatMonth < DatMonth2)  
        {  
            if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)  
            {  
                if (sReturn.Length > 0) sReturn += ",";  
                sReturn += DatMonth.ToShortDateString();  
            }  
            DatMonth = DatMonth.AddDays(1.0);  
        }  
        return sReturn;  
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM