如何在具有以下参数的sql中计算结束日期

mzillmmw  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(320)

我需要计算rrule的结束日期:
每周星期二和星期四,共5周:
DTS启动;tzid=美国-eastern:19970902t090000 rrule:freq=每周;直至=19971007t000000z;wkst=su;byday=tu、th或rrule:freq=weekly;计数=10;wkst=su;byday=tu,时间
==>(1997年美国东部时间上午9:00)9月2、4、9、11、16、18、23、25、30日;10月2日
我需要在mssql中完成它。任何帮助都将不胜感激。

nafvub8i

nafvub8i1#

请参见以下内容:

DateTime date = new DateTime(2020, 7,17);

DayOfWeek dayOfWeek = date.DayOfWeek;

int addOffset = 0;
//get first tuesday or thursday
switch (dayOfWeek)
{
    case DayOfWeek.Sunday :
        addOffset = 2;
        break;
    case DayOfWeek.Monday:
        addOffset = 1;
        break;
    case DayOfWeek.Tuesday:
        addOffset = 0;
        break;
    case DayOfWeek.Wednesday:
        addOffset = 1;
        break;
    case DayOfWeek.Thursday:
        addOffset = 0;
        break;
    case DayOfWeek.Friday:
        addOffset = 4;
        break;
    case DayOfWeek.Saturday:
        addOffset = 3;
        break;
}
DateTime startDate = date.AddDays(addOffset);
DateTime currentDate = startDate;

int numberOfWeeks = 5;
for (int i = 0; i < numberOfWeeks; i++)
{
    for(int j = 0; j < 2; j++)
    {
        Console.WriteLine(currentDate);
        if (currentDate.DayOfWeek == DayOfWeek.Tuesday)
        {
            currentDate = currentDate.AddDays(2);
        }
        else
        {
            currentDate = currentDate.AddDays(5); //from thursday to tuesday
        }
    }
}
Console.ReadLine();

相关问题