开发者

Calculating Weekly Rotating Schedule

Ok, I am not sure how to approach this... I am using an open source CMS (Umbraco) and want to create a macro th开发者_高级运维at rotates content every three weeks. So basically I have three documents and I want to show document 1, 2 or 3 each week (total three week rotation) based on a given start date... Any suggestions? I suck at working with dates in C#!

I don't need any specific code other than a C# function that spits back week 1, 2 or 3 given start date...


This works, although you may want to adjust it if you always want the weeks to start on a given day (e.g. Sunday).

        DateTime startDate = new DateTime(2011, 1, 1).Date;
        DateTime now = DateTime.Now.Date;
        int days = (int)now.Subtract(startDate).TotalDays;
        int weeks = days / 7;
        Console.WriteLine((weeks % 3) + 1);


DateTime begin = new DateTime(2011, 03, 07);
TimeSpan timeSpan = DateTime.Now - begin;
switch (((int) timeSpan.TotalDays / 7) % 3)
{
    case 0:
        break;
    case 1:
        break;
    case 2:
        break;
    default:
        throw new Exception();
}

The first '/ 7' gives you the week number, the '% 3' tells you if you're in the 1st, 2nd or 3rd.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜