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......

Open in new window

AdnanAsked:
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.

Jeff EdmundsApplication 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";
        Console.WriteLine(answer);
        Console.ReadLine();

Open in new window


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

Your issues matter to us.

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

Start your 7-day free trial
NorieVBA ExpertCommented:
Perhaps
var yrs = res/12;
    
var mths = res % 12;

Console.WriteLine (string.Format("{0} years {1} months", yrs,mths));

Open in new window

0
AdnanAuthor Commented:
Thanks, solved my issue, but why using Int64 and not just Int?
0
Jeff EdmundsApplication 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.