计算总年数 [英] To calculate total number of years

查看:111
本文介绍了计算总年数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我必须计算加入的总年数(即,我的加入日期为2009年8月1日
我必须计算到日期为止的年数,并在下拉菜单中显示如下,
2009
2010
2011
2012

Hi
I have to calculate the total number of years (i.e) my date of joining is on 08-01-2009
i have to calculate the number of years till date and display it in the dropdown as follows
2009
2010
2011
2012

推荐答案

使用和扩展方法:

Use and extension method:

    public static class ExtensionMethods
    {
        public static IEnumerable<int> GetYearsUntilNow(this DateTime startDate)
        {
            for (int year = startDate.Year; year <= DateTime.Now.Year; ++year)
            {
                yield return year;
            }
        }
    }
</int>



然后称呼它:



Then call it:

var startDate = new DateTime(2009, 1, 8);
var years = startDate.GetYearsUntilNow();


一个简单的解决方案:

int Total_year = System.DateTime.Year.Now-year_of_joining
a rough solution for your problem:

int Total_year=System.DateTime.Year.Now-year_of_joining


这篇关于计算总年数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆