简体   繁体   English

一周的自定义开始日和结束日的逻辑。 (C#)

[英]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 week and end day of a week(C#), 我们有一个独特的要求,我们有一周的开始日和一周的结束日(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). 与现有的Date Time类相反,其开始日是星期日,结束日是星期六,我们有星期三作为我们的开始日和星期二我们的结束日(这将因客户而异)。

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 a)对于给定日期,获取一周的开始日期和结束一周的日期

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 例如:基于上述情况,如果当天是2010年9月23日(星期四),我们需要将2010年9月22日(周三)作为我们的开始日和2010年9月28日(星期二)作为我们的结束日

i apologize there is a correction the end day should be Tuesday (7 days a week) 我很抱歉有一个更正,结束日应该是星期二(每周7天)

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. 绝不聪明或超高效,尽管循环迭代次数不超过7次。

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

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