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 7554729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:25:03+00:00 2026-05-30T11:25:03+00:00

My previous question got a lot cons. I will try again, with a code

  • 0

My previous question got a lot cons. I will try again, with a code and why it not work.

For example, we have event that will go every 3 month. Event have DateStart, DataEnd and Periodicity.
For example, we have a record:

start = 02/23/2012 22:00:00;
end = 12/31/2012 23:30:00;
periodicity = 3;

Method must return true when current month = February, May, August, November.

Code:

public bool MonthPeriodicityChecker (DateTime start, DateTime end, DateTime dateToCheck, int periodicity)
{
     var monthDiff = dateToCheck.Month - startDate.Month;
     return (monthDiff-1) % periodicity == 0;
}

This code return true if month in dateToCheck equals April, July, October. It’s skip first month.

UPDATE:

Damn, sorry to all. In the beyond, I have loop that sumulated dates. And this loop start with 1 and add this 1 to start. And next date was 24 February. Therefore, the February not prints( there are checks for number(23) of month too) in my program. Sorry and thanks.

  • 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-30T11:25:05+00:00Added an answer on May 30, 2026 at 11:25 am

    If you want this to trigger in February, May, August and November, you shouldn’t be subtracting one from the difference. Just use:

    return (monthDiff % periodicity) == 0;
    

    When the months are equal (February), 0 % 3 == 0. When you’re a multiple of three months out (such as August), 6 % 3 == 0.

    And I’m not sure about C# but some languages don’t act as you may expect when taking the modulus of a negative number, so I’d be doubly safe with:

    public bool MonthPeriodicityChecker (DateTime start, DateTime end,
        DateTime dateToCheck, int periodicity)
    {
        var monthDiff = dateToCheck.Month - startDate.Month + 12; // make positive
        return (monthDiff % periodicity) == 0;
    }
    

    However, keep in mind that, because you’re only using the month of the year in your calculations, this probably won’t work as expected if you run more than 12 months and your periodicity is not something that divides into 12 cleanly.

    Example with starting December 2010 for every five months.

    • December 2010 is okay since the difference is zero: 0 % 5 == 0.
    • May 2011 is okay since the difference between May and December is five months: 5 % 5 == 0.
    • October 2011 is okay since the difference between October and December is ten months: 10 % 5 == 0.
    • March 2012 is not okay. The difference between March and December is three months, not fifteen as has happened in reality: 3 % 5 == 3.

    The upshot of that is that you’ll fire in May, October and December in every year.

    You can fix that little problem (if it is a problem for you) by ensuring the years are taken into account, something like:

    public bool MonthPeriodicityChecker (DateTime start, DateTime end,
        DateTime dateToCheck, int periodicity)
    {
        // assuming Month returns 1-12 here which is the case for C# I think.
        var monthDiff = (dateToCheck.Year * 12 + dateToCheck.Month - 1)
            - (startDate.Year * 12 + startDate.Month - 1);
        return (monthDiff % periodicity) == 0;
    }
    

    That also gets rid of the need for adding 12 to get a positive number since the advancing years will ensure that (a jump from December to January will now give you 1 rather than -11), assuming that dateToCheck.Month will always be greater than or equal to startDate.Month.

    If there’s the possibility that dateToCheck may be less startDate, you probably want to check that first and return false as the first step in the above function.

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

Sidebar

Related Questions

to those that may have read my previous question that got solved a couple
This question is related to a previous question of mine That's my current code
Previous question linky: MySQL Left Joins Very kindly we got the following code working:
I've got this form working, but according to my previous question it might not
In previous question I got comment about Oracle statistics: Oracle doesn't know that 50M
Actually my previous question got me thinking and I realized that reversing a Dictionary
Thanks to the help in a previous question , I've got the following code
In my previous question I got an excellent answer that helped me detect where
In my previous question .I have got the solution to check out which data
This is an error that came up after I got a previous question here

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.