Quantcast
Channel: Difference in months between two dates - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by Miguel for Difference in months between two dates

$
0
0

In case you just care about the month and year and want to touch both dates (for example you want to go thru JAN/2021 to AGO/2022) you can use this:

int numberOfMonths= (Year2 > Year1 ? ( Year2 - Year1 - 1) * 12 + (12 - Month1) + Month2 + 1 : Month2 - Month1 + 1); 

Example:

Year1/Month1: 2021/10   Year2/Month2: 2022/08   numberOfMonths = 11;

Or same year:

Year1/Month1: 2021/10   Year2/Month2: 2021/12   numberOfMonths = 3;

If you just want to touch one of them remove both + 1.


Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>