如何根据给定的数字计算年和月 C#

how to calculate years and month from given number c#

我需要根据给定的数字计算年和月。我该怎么做? 例如: 我给出:26 我需要得到结果:2 年 2 个月 请帮忙

除非您有更具体的要求,否则它应该像整数除法和 Remainder operator %

一样简单
var input = 26;
var years = input / 12;
var months = input % 12;

Console.WriteLine($"{years} years and {months} months");

输出

2 years and 2 months

private static (int Years, int Months) GetYearsAndMonths(int input) 
   => (input / 12, input % 12);

...

var result = GetYearsAndMonths(26);

Console.WriteLine($"{result.Years} years and {result.Months} months");

或鲜为人知的方法Math.DivRem Method as supplied by @Charlieface

Calculates the quotient of two numbers and also returns the remainder in an output parameter.