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

  • SEARCH
  • Home
  • 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 7061853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:31:27+00:00 2026-05-28T04:31:27+00:00

Sounds like a homework? no it’s not. I worked up the logic for this

  • 0

Sounds like a homework? no it’s not. I worked up the logic for this but not such a performant one when dates are span over years.
Basically here is how it should work,

StartDate: 1/1/2012

FinishDate: 1/10/2012 

RecurringInterval: 2 ( In days)

Output would be:

1/6/2012 if Todays date (Date.Now) is 1/5/2012 ( Assuming format MM/dd/yyyy). Check would end when finish date is reached. If no dates match within given time period, today’s Date must be returned. Dead simple but not a efficient one.

What is wrong with this?

if (!_isRecurring)
    return DateTime.UtcNow;
DateTime initialDate = _startDate;
DateTime finalDate = _finishDate;
int recurringDays = _recurringInteral;
/*
 * start Date + recurring interval falls between start date and finishdate then get its date
 */
do
{
    //add recurring day to start date
    initialDate = initialDate.AddDays(recurringDays);
    //check if it falls in between start days and end days
     if(initialDate  <= finalDate)
    break;            
} while (initialDate <= finalDate);
//return the first occurance of the recurring day
return initialDate;
  • 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-28T04:31:28+00:00Added an answer on May 28, 2026 at 4:31 am

    A little arithmetic should save the day (pun intended):

    var start = new DateTime(2012, 1, 1);
    var end = new DateTime(2012, 10, 1);
    var interval = 2; // days
    
    var today = DateTime.Today;
    var diff = (int)((today - start).TotalDays);
    var mod = diff % interval;
    var correction = TimeSpan.FromDays((mod > interval / 2 ? interval : 0) - mod);
    var result = today + correction > end ? today : today + correction;
    Console.Out.WriteLine("Result is: {0}", result);
    

    See it in action.

    What this does is calculate how many days away from a “recurrence spot” today is (variable mod). This is obviously going to be a number >= 0 and < interval. If it’s half the interval or less, it means the closest recurrence spot is earlier than today, in which case subtract mod days from today to find the spot. If it’s greater than half the interval, it means that we need to add interval - mod days to find the spot (which is going to be in the future).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds like a homework assignment, but it isn't. Lately I've been
This sounds like a contradiction in terms, or flamebait, but I'm hoping its not
This sounds like a simple one but I can't find the solution anywhere. How
This sounds like a weird title and probably not stated too well. But here's
This sounds like it should be easy but for some reason it's not work
This sounds like such a dumb question but I've spent too long looking for
Sounds like a weird question, but say I have something like this: $.post( /myajax.php,
This sounds like a look-up-in-the-manual question to me, but I can't find it. Suppose
This sounds like a really simple question, but I am new to PHP. If
Sorry if this sounds like a really stupid question, but I need to make

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.