需要跟踪游戏的当前季节(整数),该季节每1个月增加1。
在运行时如何计算自固定日期以来一个月内的第一个次数?
范例:第1季开始的固定日期是01/04/2015
。用户在处使用该应用程序09/09/15
。在这一点上,应用程序需要知道,当前的季节现在是season 6
。
这等效于计算以月为单位的差额,因为这样就不计算当前日期。因此,这里的答案也将适用于您的特定问题:https : //stackoverflow.com/a/1526004/2606322
int months = (now.Month - start.Month) + 12 * (now.Year - start.Year);
但是存在细微的差异。如果开始日期是1日,则无论是否要计算,都必须再加上一个月。
int months = (now.Month - start.Month) + 12 * (now.Year - start.Year) + (start.Day == 1 ? 1 : 0);
或者,如果您希望基于1的计数,只需添加1,就像您在示例中所做的那样。
int months = (now.Month - start.Month) + 12 * (now.Year - start.Year) + 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句