通过循环只完成几个月 [英] Only complete months through a loop

查看:95
本文介绍了通过循环只完成几个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,



我试图在两个日期之间循环,并且只计算从1开始并在30或31结束的那些(完整)月份。所以,例如,2014年1月20日到2014年5月20日之间我需要回到3号。



Thx任何帮助。

Hy,

I am trying to loop between two dates, and count ONLY those (complete) months which start on 1. and finish on 30 or 31. So, for example, between 20/01/2014 and 20/05/2014 I need to get back number 3.

Thx for any help.

推荐答案

尝试:

Try:
private int GetWholeMonths(DateTime start, DateTime end)
    {
    if (start.Day != 1)
        {
        start = new DateTime(start.Year, start.Month, 1).AddMonths(1);
        }
    return (end.Month - start.Month) + 12 * (end.Year - start.Year);
    }


这篇关于通过循环只完成几个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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