Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:30:42+00:00 2026-05-23T04:30:42+00:00

Hello I’m a trying to create a feature where an user an add an

  • 0

Hello I’m a trying to create a feature where an user an add an items that will be repeated on center intervals. But I am having some problems wrapping my head around the math, so I was hoping some of you guys have done something like this before.

It is based on the Google calendar, where an event can be repeated by 4 different methods.

  1. Daily
  2. Weekly
  3. Monthly
  4. Yearly

Then user then defines what type of repeating the user wants, daily are simple, it is just each day.
Now weekly is more tricky, as the user can select 2 options

  1. Week interval (Repeats every other week, or third and so on)
  2. What day on the week (Monday, Thursday and so on)

Monthly and Yearly only have 1 option

  1. Month/Year interval

Beside this data do I have the following variables.

  1. Time (The time of day the item are to be added)
  2. StartsOn (The day the item shale start repeating)
  3. Occurrence (The number of times the item have been executed)
  4. LastRun (Last time the item was executed)
  5. NextRun (The next time the item is to be executed)

So I do not have to display future items, is the plan to loop though all items where the NextRun is equel to now, and after the item have been executed will the NextRun be calculated.
I have tried myself but it seems to become overly complex, so I where hoping there was an already completed solution out there, or one close to, or simply just a hint to get it on track.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The result, this have been tested and workes
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Itenso.TimePeriod;

namespace ShoppingList.Library.Objects
{
    public class RepeatingItem
    {
        public int? Id { get; set; }
        public int ListId { get; set; }
        public string Item { get; set; }
        public int Quantity { get; set; }
        public RepeatsType Repeats { get; set; }
        public string RepeatsVar { get; set; }
        public TimeSpan Time { get; set; }
        public DateTime StartsOn { get; set; }
        public EndsType Ends { get; set; }
        public string EndsVar { get; set; }
        public int Occurrences { get; set; }
        public DateTime? LastRun { get; set; }
        public DateTime? NextRun { get; set; }

        public enum RepeatsType
        {
            Daily = 0,
            Weekly = 1,
            Monthly = 2,
            Yearly = 3,
        }
        public enum EndsType
        {
            Never = 0,
            After = 1,
            On = 2
        }

        public DateTime? CalculateNextRun()
        {
            DateTime? next = null;

            if (Repeats == RepeatsType.Daily)
                next = HandelDailyRepeating();
            else if (Repeats == RepeatsType.Weekly)
                next = HandelWeeklyRepeating();
            else if (Repeats == RepeatsType.Monthly)
                next = HandelMonthlyRepeating();
            else if (Repeats == RepeatsType.Yearly)
                next = HandelYearlyRepeating();

            if (Ends != EndsType.Never && next != null)
            {
                if (Ends == EndsType.After)
                {
                    if (Occurrences >= int.Parse(EndsVar))
                        next = null;
                }
                else if (Ends == EndsType.On)
                {
                    if (next >= DateTime.Parse(EndsVar))
                        next = null;
                }
            }
            return next;
        }

        private DateTime? HandelDailyRepeating()
        {
            DateTime last;
            // If there where a last run no problem.
            // but we are not sure that the time on
            // start on are right.
            if (LastRun != null)
                last = GetDateTime((DateTime)LastRun, Time);
            else
                last = GetDateTime(StartsOn, Time);

            DateTime next;
            while (last < DateTime.UtcNow || last == LastRun)
            {
                last = last.AddDays(1);
            }
            return last;
        }
        private DateTime? HandelWeeklyRepeating()
        {
            DateTime last;
            // If there where a last run no problem.
            // but we are not sure that the time on
            // start on are right.
            if (LastRun != null)
                last = GetDateTime((DateTime)LastRun, Time);
            else
                last = GetDateTime(StartsOn, Time);

            string[] split = RepeatsVar.Split(';');
            int recuringInterval = int.Parse(split[0]);
            if (recuringInterval > 52)
                recuringInterval = 52;

            DayOfWeek[] weekDays = new DayOfWeek[split.Count() - 1];
            for (int i = 1; i < split.Count(); i++)
            {
                weekDays[i-1] = (DayOfWeek)int.Parse(split[i]);
            }

            int days = 0;
            bool validFound = false;
            while (!validFound && days <= (7 * (recuringInterval + 1)))
            {

                if (last >= DateTime.UtcNow && IsWeekRecuringDay(StartsOn, last, recuringInterval, weekDays)
                    && last != LastRun)
                {
                    return last;
                }
                else
                {
                    last = last.AddDays(1);
                    if(last > DateTime.UtcNow) days++;
                }
            }
            return null;
        }
        private DateTime? HandelMonthlyRepeating()
        {
            DateTime last;
            if (LastRun != null)
                last = GetDateTime((DateTime)LastRun, Time);
            else
                last = GetDateTime(StartsOn, Time);

            int recuringInterval = int.Parse(RepeatsVar);

            int c = 0;
            bool validFound = false;
            while (!validFound && c <= (recuringInterval + 1))
            {
                if (last >= DateTime.UtcNow && IsMonthlyRecuringDay(StartsOn, last, recuringInterval)
                    && last != LastRun)
                {
                    return last;
                }
                else
                {
                    last = last.AddMonths(1);
                    if (last > DateTime.UtcNow) c++;
                }
            }
            return null;
        }
        private DateTime? HandelYearlyRepeating()
        {
            DateTime last;
            // If there where a last run no problem.
            // but we are not sure that the time on
            // start on are right.
            if (LastRun != null)
                last = GetDateTime((DateTime)LastRun, Time);
            else
                last = GetDateTime(StartsOn, Time);

            int recuringInterval = int.Parse(RepeatsVar);

            int c = 0;
            bool validFound = false;
            while (!validFound && c <= (recuringInterval + 1))
            {
                if (last >= DateTime.UtcNow && IsYearlyRecuringDay(StartsOn, last, recuringInterval)
                    && last != LastRun)
                {
                    return last;
                }
                else
                {
                    last = last.AddYears(1);
                    if (last > DateTime.UtcNow) c++;
                }
            }
            return null;
        }

        public bool IsWeekRecuringDay(DateTime start, DateTime test, int recuringInterval, params DayOfWeek[] weekDays)
        {
            if (test < start || recuringInterval <= 0)
                return false;

            bool isValidDayOfWeek = false;
            DayOfWeek testDayOfWeek = test.DayOfWeek;

            // Is the given day a valid day
            foreach (DayOfWeek weekDay in weekDays)
            {
                if (weekDay == testDayOfWeek)
                {
                    isValidDayOfWeek = true;
                    break;
                }
            }

            // If the day is not in the list, no need to go further
            if (!isValidDayOfWeek)
                return false;

            DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
            return (dateDiff.Weeks % recuringInterval) == 0;
        }
        public bool IsMonthlyRecuringDay(DateTime start, DateTime test, int recuringInterval)
        {
            if (test < start || recuringInterval <= 0)
                return false;
            DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
            return (dateDiff.Months % recuringInterval) == 0;
        }
        public bool IsYearlyRecuringDay(DateTime start, DateTime test, int recuringInterval)
        {
            if (test < start || recuringInterval <= 0)
                return false;
            DateDiff dateDiff = new DateDiff(GetDateTime(start, new TimeSpan(0, 0, 0)), GetDateTime(test, new TimeSpan(0, 0, 0)));
            return (dateDiff.Years % recuringInterval) == 0;
        }

        private DateTime GetDateTime(DateTime d, TimeSpan t)
        {
            return new DateTime(d.Year, d.Month, d.Day, t.Hours, t.Minutes, t.Seconds); ;
        }
        private TimeSpan GetTimeSpanFromDateTime(DateTime s)
        {
            DateTime zero = new DateTime(s.Year, s.Month, s.Day, 0, 0, 0);
            return s - zero;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:30:43+00:00Added an answer on May 23, 2026 at 4:30 am

    One approach is to check the week difference between the test and start date.

    The following sample uses the the DateDiff class of the Time Period Library for .NET to calculate the week differences:

    // ----------------------------------------------------------------------
    public bool IsWeekRecuringDay( DateTime start, DateTime test, int recuringInterval, params DayOfWeek[] weekDays )
    {
      if ( test < start || recuringInterval <= 0 )
      {
        return false;
      }
    
      bool isValidDayOfWeek = false;
      DayOfWeek testDayOfWeek = test.DayOfWeek;
      foreach ( DayOfWeek weekDay in weekDays )
      {
        if ( weekDay == testDayOfWeek )
        {
          isValidDayOfWeek = true;
          break;
        }
      }
      if ( !isValidDayOfWeek )
      {
        return false;
      }
    
      DateDiff dateDiff = new DateDiff( start, test );
      return ( dateDiff.Weeks % recuringInterval ) == 0;
    } // IsWeekRecuringDay
    

    And here the usage:

    // ----------------------------------------------------------------------
    public void WeekRepeatSample()
    {
      DateTime start = new DateTime( 2011, 06, 1 );
      DayOfWeek[] weekDays = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Thursday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 08 ), 2, weekDays ) ); // false
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 11 ), 2, weekDays ) ); // false
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 15 ), 2, weekDays ) ); // true
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 18 ), 2, weekDays ) ); // false
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 22 ), 2, weekDays ) ); // false
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 25 ), 2, weekDays ) ); // false
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 06, 29 ), 2, weekDays ) ); // true
      Console.WriteLine( "IsWeekRecuringDay: {0}", IsWeekRecuringDay( start, new DateTime( 2011, 07, 02 ), 2, weekDays ) ); // false
    } // WeekRepeatSample
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there i'm trying to create a form with Zend_Form that will enable my
I'm trying to create a setup project using WIX that will allow me to
I am trying to create a Hello world application in Silverlight 3.0, but when
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I am trying to create my hello world windows app in WPF. What should
I am trying to create a Hello World SWT application using Eclipse. I follow
I am trying to create a Socket Hello World program. I would ideally like
I am trying to create a small function that returns the number of spaces
I am trying to create a magento shopping cart module but things arent working
I am trying to learn shell scripting and trying to create a user defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.