计算C#中两个日期之间的月份差

本文介绍了一种在C#中计算两个日期之间精确月份差的方法,考虑到不同月份天数不一致以及二月的特殊情况。通过将每个月份视为0到1之间的数值,并利用DaysInMonth内置方法,可以准确计算出如3.75个月这样的差值。
摘要由CSDN通过智能技术生成

Hi,

你好

There are a lot of written methos of getting the difference between two dates, but I couldn't find the one I needed. I need to have a real difference like 3.75 months between the two dates.

有很多书面方法来获取两个日期之间的差额,但我找不到我需要的日期。 我需要在两个日期之间有一个真正的区别,例如3.75个月。

Not every month has the same amount of days and the February is also an issue here. So I made myself a method doing this:

并非每个月都有相同的天数,因此2月也是一个问题。 所以我自己做了一个方法:

/// <summary>
/// Get months difference between two dates
/// </summary>
/// <param name="from">from date</param>
/// <param name="to">to date</param>
/// <returns>months (double) between </returns>
internal static double GetMonths(DateTime from, DateTime to)
{
   
	/// |-------X----|---------------|--------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值