How to calculate year and month out of int value....

Hi

How can i calculate year /month out of int value,  if i have a value "16", that means it is 1 year and 4 months.
Whatever result i get in int iw ant it to be calculated in year and months

How can i do that?

`````` var currDate = DateTime.Now.ToShortDateString();

DateTime.TryParseExact(ansiennitetsdatoberegnet, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out startDate);
DateTime.TryParseExact(currDate, formats, CultureInfo.CurrentCulture, DateTimeStyles.None, out currentDate);

result = ((startDate.Year - currentDate.Year) * 12) + startDate.Month - currentDate.Month;
var result2 = 12 * (startDate.Year - currentDate.Year) + startDate.Month - currentDate.Month;
var res = Math.Abs(result); //res returns 16 ,  1 year, 4 months......
``````
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Application Developer/SQL DBACommented:
You're looking for the modulus operator:  %

``````        Int64 originalNumber = 16;
Int64 numYears = originalNumber / 12;
Int64 numMonths = originalNumber % 12;
string answer = numYears + " Year, " + numMonths + " Months";
``````

The integer value gives you the whole number (in this case the number of years). The modulus gives you the remainder (in this case then number of months.)
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

VBA ExpertCommented:
Perhaps
``````var yrs = res/12;

var mths = res % 12;

Console.WriteLine (string.Format("{0} years {1} months", yrs,mths));
``````
0
Author Commented:
Thanks, solved my issue, but why using Int64 and not just Int?
0
Application Developer/SQL DBACommented:
You could really use either.  With numbers that small, "int" or "Int32" probably makes more sense. It was more just to show the code using an integer value.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.