-1

The need is to keep track of the current season (as an integer) of a game, that increments by one at every 1st of a month.

How is it possible at runtime to count the amount of times it has been the 1st in a month since a fixed date?

Example: Fixed date for season 1 start is 01/04/2015. A user uses the application at 09/09/15. At this point the application needs to know, that the current season now is season 6.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Jesper
  • 3
  • 2

2 Answers2

-1

This is equivalent to computing the difference in months, since then the current day is not counted. So this answer here will apply to your specific problem as well: https://stackoverflow.com/a/1526004/2606322

int months = (now.Month - start.Month) + 12 * (now.Year - start.Year);

There is a subtile difference however. If your start date is on a 1st, you have to add another month, whether or not you want to count that.

int months = (now.Month - start.Month) + 12 * (now.Year - start.Year) + (start.Day == 1 ? 1 : 0);

Or, if you want to have a 1 based count, just add 1, as you probably do in your example.

int months = (now.Month - start.Month) + 12 * (now.Year - start.Year) + 1;
Community
  • 1
  • 1
azt
  • 2,100
  • 16
  • 25
  • If that link contains the answer, why not vote to close the question as a duplicate instead of reposting? – DavidG Sep 09 '15 at 09:24
  • The question is different, since he wants the number of times the 1st has passed, as opposed to the difference in months. The answer is the same, but I feel there is a significant semantic difference. – azt Sep 09 '15 at 09:27
  • @DavidG: Pointed out the difference now. Hope that is fine with you. There *is* a difference of +/-1 depending on how you define it. Just wanted to give credit to a similar algorithm. – azt Sep 09 '15 at 09:35
-1

If i understand the question correctly

DateTime dt1 = new DateTime(2014, 4, 1);
DateTime dt2 = DateTime.Now;
// since you have taken the "1" start is 01/04/2015.
int Diff = (dt2.Month - dt1.Month) + ((dt2.Year - dt1.Year) * 12) + 1; 
Mohit S
  • 13,723
  • 6
  • 34
  • 69