使用C#中的DateTime计算事件的复发 [英] Calculating recurrence of events using DateTime in C#

查看:158
本文介绍了使用C#中的DateTime计算事件的复发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列重复发生的事件。我需要能够计算这些事件何时发生在接下来的5周左右。



此系统将通知用户本月内将发生这些事件。



例如:



Event1




  • = 2011年12月19日星期一

  • 复发模式 =星期一/四周



Event2




  • 开始日期 = 2012年2月3日

  • 复发模式 =周三/每三周



3月22日 - 在接下来的5周内,活动1&



如果能够检测是否是圣诞节,那么事件将会落到另一天,这也是有用的。



我正在使用.NET MVC2,但我猜是这是偶然的。



感谢任何帮助

解决方案

这样的事情应该是诀窍:

  //枚举各种模式
public enum OccurrenceRate
{
每周,
每周两次,
每月
}

public static List&Date日期& GetOccurrences(DateTime startDate,DateTime endDate,OccurrenceRate rate)
{
列表< DateTime>出现=新List< DateTime>();

var nextDate = startDate;

while(true)
{
if(nextDate< = endDate)
{
occurrences.Add(nextDate);
}
else
{
break;
}

switch(rate)
{
case OccurrenceRate.Weekly:
{
nextDate = nextDate.AddDays(7);
break;
}
case OccurrenceRate.Fortnightly:
{
nextDate = nextDate.AddDays(14);
break;
}
case OccurrenceRate.Monthly:
{
nextDate = nextDate.AddMonths(1);
break;
}
}
}

返回事件;
}

调用代码示例:

  DateTime startDate = DateTime.Now; 
DateTime endDate = startDate.AddDays(35); // 5周

var dates = GetOccurrences(startDate,startDate.AddDays(35),OccurrenceRate.Weekly);
dates.ForEach(date =>
{
Console.WriteLine({0:yyyy-MM-dd},date);
});


I have a set of events that reoccur. I need to be able to calculate when these events occur with the next 5 weeks or so.

This system will inform the user that within this month, these events will occur..

For example:

Event1

  • Date Started = Monday 19th Dec 2011
  • Recurrence Pattern = Monday/Fortnightly

Event2

  • Date Started = Thursday 3rd Feb 2012
  • Recurrence Pattern = Thursday/Every Three Weeks

It's now 22 March - within the next 5 weeks what dates will Events 1 & 2 fall on.

It would also be useful to be able to detect if it's Xmas then the event will fall onto another day.

I'm using .NET MVC2 but I guess that's incidental.

Thanks for any assistance

解决方案

Something like this should do the trick:

//enum for various patterns
public enum OccurrenceRate
{
    Weekly,
    Fortnightly,
    Monthly
}

public static List<DateTime> GetOccurrences(DateTime startDate, DateTime endDate, OccurrenceRate rate)
{
    List<DateTime> occurrences = new List<DateTime>();

    var nextDate = startDate;

    while (true)
    {
        if (nextDate <= endDate)
        {
            occurrences.Add(nextDate);
        }
        else
        {
            break;
        }

        switch (rate)
        {
            case OccurrenceRate.Weekly:
            {
                nextDate = nextDate.AddDays(7);
                break;
            }
            case OccurrenceRate.Fortnightly:
            {
                nextDate = nextDate.AddDays(14);
                break;
            }
            case OccurrenceRate.Monthly:
            {
                nextDate = nextDate.AddMonths(1);
                break;
            }
        }
    }

    return occurrences;
}

Example of calling code:

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(35); //5 weeks

var dates = GetOccurrences(startDate, startDate.AddDays(35), OccurrenceRate.Weekly);
dates.ForEach(date =>
{
    Console.WriteLine("{0:yyyy-MM-dd}", date);
});

这篇关于使用C#中的DateTime计算事件的复发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆