如何在C#中计算两个日期之间的月数 [英] How can I calculate the numbers of month between two dates in C#
本文介绍了如何在C#中计算两个日期之间的月数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何计算两个日期之间的月数.有什么方法可以在C#中进行计算吗?
I would like to know how to calculate the numbers of Month between two dates. Is there any method to calculate it in C#?
Eg1. Date1 = "2011/11/01"
Date2 = "2012/02/01"
Result. Numbers of Month =3
Eg2. Date1 = "2012/01/31"
Date2 = "2012/02/01"
Result. Numbers of Month =1
Eg3. Date1 = "2012/01/01"
Date2 = "2012/02/28"
Result. Numbers of Month =1
推荐答案
这将使月份之间有所不同:
This will give difference between months:
int months = (Date2.Year - Date1.Year) * 12 + Date2.Month - Date1.Month;
这篇关于如何在C#中计算两个日期之间的月数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文