开发者

logic for Custom Start Day and End Day for a week. (c#)

We have a unique requirment where we have our own start day of a wee开发者_运维技巧k and end day of a week(C#),

Contrary to the existing Date Time class whose start day is Sunday and end day is Saturday, we have Wednesday as our start day and Tuesday our EndDay (and this will vary per client).

and we need to implement the following logic

a) for a given date get the start Date of a week and end Date of a week

Ex: based on the above if the current day is 23/Sep/2010 (Thur) we need to get 22/Sep/2010 (Wed) as our Start Day and 28/Sep/2010 (Tue) as our end day

i apologize there is a correction the end day should be Tuesday (7 days a week)

Can anyone help here?

Thanks


Couldn't you just do

(dt.DayOfWeek + delta) % 7

?


Here is my inelegant crack at it:

public static void CalculateWeek (out DateTime WeekStart, out DateTime WeekEnd, DateTime InputDate)
    {
        DateTime tempDT = InputDate;

        while (tempDT.DayOfWeek != DayOfWeek.Wednesday)
        {
            tempDT = tempDT.AddDays(-1);
        }

        WeekStart = tempDT.Date;

        while (tempDT.DayOfWeek != DayOfWeek.Tuesday)
        {
            tempDT = tempDT.AddDays(1);
        }

        WeekEnd = tempDT.Date;
    }

By no means clever or superefficient, although the loops will iterate no more than a trivial 7 times.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜