本文翻译自:Difference in months between two dates

How to calculate the difference in months between two dates in C#? 如何计算C#中两个日期之间的月份差异?

Is there is equivalent of VB's DateDiff() method in C#. C#中是否有等效于VB的DateDiff()方法。 I need to find difference in months between two dates that are years apart. 我需要找出相隔数年的两个日期之间的月份差异。 The documentation says that I can use TimeSpan like: 文档说我可以像这样使用TimeSpan

TimeSpan ts = date1 - date2;

but this gives me data in Days. 但这给了我几天的数据。 I don't want to divide this number by 30 because not every month is 30 days and since the two operand values are quite apart from each other, I am afraid dividing by 30 might give me a wrong value. 我不想将这个数字除以30,因为不是每个月都有30天,并且由于两个操作数值彼此相距甚远,所以我怕除以30可能会给我一个错误的值。

Any suggestions? 有什么建议么?


#1楼

参考:https://stackoom.com/question/JSoT/两个日期之间月份的差异


#2楼

This is from my own library, will return the difference of months between two dates. 这来自我自己的库,将返回两个日期之间的月份差。

public static int MonthDiff(DateTime d1, DateTime d2)
{int retVal = 0;// Calculate the number of years represented and multiply by 12// Substract the month number from the total// Substract the difference of the second month and 12 from the totalretVal = (d1.Year - d2.Year) * 12;retVal = retVal - d1.Month;retVal = retVal - (12 - d2.Month);return retVal;
}

#3楼

To be able to calculate the difference between 2 dates in months is a perfectly logical thing to do, and is needed in many business applications. 能够计算两个月中两个日期之间的差是一件很合理的事情,并且在许多业务应用程序中都需要。 The several coders here who have provided comments such as - what's the difference in months between "May 1,2010" and "June 16,2010, what's the difference in months between 31 December 2010 and 1 Jan 2011? -- have failed to understand the very basics of business applications. 此处提供评论的几位编码人员,例如-“ 2010年5月1日”和“ 2010年6月16日之间的月份有什么区别,2010年12月31日至2011年1月1日之间的月份有什么区别?”-无法理解业务应用程序的基础知识。

Here is the answer to the above 2 comments - The number of months between 1-may-2010 and 16-jun-2010 is 1 month, the number of months between 31-dec-2010 and 1-jan-2011 is 0. It would be very foolish to calculate them as 1.5 months and 1 second, as the coders above have suggested. 这是上述2条评论的答案-2010年5月1日至2010年6月16日之间的月数为1个月,2010年12月31日至2011年1月1日之间的月数为0。如上面的编码人员所建议的那样,将它们计算为1.5个月零1秒将是非常愚蠢的。

People who have worked on credit card, mortgage processing, tax processing, rent processing, monthly interest calculations and a vast variety of other business solutions would agree. 从事信用卡,抵押贷款处理,税收处理,租金处理,每月利息计算以及其他各种业务解决方案工作的人都会同意。

Problem is that such a function is not included in C# or VB.NET for that matter. 问题在于,C#或VB.NET中不包含此类功能。 Datediff only takes into account years or the month component, so is actually useless. Datediff仅考虑年份或月份组成部分,因此实际上是没有用的。

Here are some real-life examples of where you need to and correctly can calculate months: 以下是一些实际示例,您需要在这些示例中正确地计算月份:

You lived in a short-term rental from 18-feb to 23-aug. 您的短期租金为2月18日至23月8日。 How many months did you stay there? 你在那里呆了几个月? The answer is a simple - 6 months 答案很简单-6个月

You have a bank acount where interest is calculated and paid at the end of every month. 您有一个银行帐户,每个月底都会计算并支付利息。 You deposit money on 10-jun and take it out 29-oct (same year). 您在6月10日存入钱,然后取出10月29日(同年)。 How many months do you get interest for? 您对多少个月感兴趣? Very simple answer- 4 months (again the extra days do not matter) 很简单的答案-4个月(同样,多余的日子也没关系)

In business applications, most of the time, when you need to calculate months, it is because you need to know 'full' months based on how humans calculate time; 在业务应用程序中,大多数时候,当您需要计算月份时,这是因为您需要基于人类如何计算时间来了解“完整”月份。 not based on some abstract/irrelevant thoughts. 并非基于某些抽象/无关的想法。


#4楼

You can have a function something like this. 您可以拥有类似这样的功能。

For Example, from 2012/12/27 to 2012/12/29 becomes 3 days. 例如,从2012/12/27到2012/12/29变为3天。 Likewise, from 2012/12/15 to 2013/01/15 becomes 2 months, because up to 2013/01/14 it's 1 month. 同样,从2012/12/15到2013/01/15变成2个月,因为直到2013/01/14都是1个月。 from 15th it's 2nd month started. 从15日开始第二个月。

You can remove the "=" in the second if condition, if you do not want to include both days in the calculation. 如果您不想在计算中同时包括两天,则可以在第二个if条件中删除“ =”。 ie, from 2012/12/15 to 2013/01/15 is 1 month. 也就是说,从2012/12/15到2013/01/15是1个月。

public int GetMonths(DateTime startDate, DateTime endDate)
{if (startDate > endDate){throw new Exception("Start Date is greater than the End Date");}int months = ((endDate.Year * 12) + endDate.Month) - ((startDate.Year * 12) + startDate.Month);if (endDate.Day >= startDate.Day){months++;}return months;
}

#5楼

public static int PayableMonthsInDuration(DateTime StartDate, DateTime EndDate)
{int sy = StartDate.Year; int sm = StartDate.Month; int count = 0;do{count++;if ((sy == EndDate.Year) && (sm >= EndDate.Month)) { break; }sm++;if (sm == 13) { sm = 1; sy++; }} while ((EndDate.Year >= sy) || (EndDate.Month >= sm));return (count);
}

This solution is for Rental/subscription calculation, where difference doesn't means to be subtraction, it's meant to be the span in within those two dates. 此解决方案用于租赁/订阅计算,其中差异并不意味着相减,而是相差两个日期之内。


#6楼

This worked for what I needed it for. 这满足了我的需要。 The day of month didn't matter in my case because it always happens to be the last day of the month. 在我的情况下,月的一天并不重要,因为它总是恰好是该月的最后一天。

public static int MonthDiff(DateTime d1, DateTime d2){int retVal = 0;if (d1.Month<d2.Month){retVal = (d1.Month + 12) - d2.Month;retVal += ((d1.Year - 1) - d2.Year)*12;}else{retVal = d1.Month - d2.Month;retVal += (d1.Year - d2.Year)*12;}Calculate the number of years represented and multiply by 12Substract the month number from the totalSubstract the difference of the second month and 12 from the total//retVal = (d1.Year - d2.Year) * 12;//retVal = retVal - d1.Month;//retVal = retVal - (12 - d2.Month);return retVal;
}

两个日期之间月份的差异相关推荐

  1. 获取Moment Js中两个日期之间的小时差异

    本文翻译自:Get hours difference between two dates in Moment Js I'm able to get the difference between two ...

  2. python横坐标如何显示为月份_如何显示给定两个日期之间的所有月份?

    我还没有足够的特权发表评论,但你的程序正在按要求运行.4周等于28天.2017年1月1日和1月29日之间的差异为28天,因此您将得到两次相同的月份. 你可能想重新定义你要解决的问题.但是,如果您只想解 ...

  3. 怎么用计算机计算年月份,如何使用Excel计算两个日期之间的月数?

    一.这可以通过DATEDIF函数完成. 二. DATEDIF函数的定义 DATEDIF函数是Excel中的隐藏函数,而不是帮助和插入公式中的隐藏函数.返回两个日期之间的年\月\日间隔的数量. DATE ...

  4. java 月份间隔_java计算两个日期之间相隔的月份(向下取整)

    最近需求里面有个需要计算两个日期之间相隔的月份,写起来还挺繁琐,需要将各种情况都要考虑到,写了一个作为以后自己的工具吧. //获取哪一天 public static int getDay(Date d ...

  5. Java 8计算两个日期之间的月份

    开始日期:"2021-08-31" 结束日期:"2021-11-30" 在上述两个日期之间的91天持续时间,期望代码返回3个月的持续时间,但是以下方法仅返回2个 ...

  6. java计算两个日期之间的月份差

    需求:计算两个日期之间相差几个月份 说明:适用于yyyyMMdd.yyyy-MM-dd.yyyy/MM/dd.yyyyMM.yyyy-MM.yyyy/MM格式的日期 package demo;impo ...

  7. Oracle 计算两个日期之间的年月、日期,月份数、天数

    目录 Oracle 计算两个日期之间的月份数.月份列表.天数.日期列表 Oracle 计算两个日期之间的月份数.月份列表.天数.日期列表 获取日期之间的月数(包括自身) 时间:2019-05-30 至 ...

  8. 计算两个日期之间的月份数

    网上搜计算两个日期之间的月份数,可能会有一大堆,像Date1 和 Date2 之间的年数相减得到一个差额然后乘以12 再加上 两日期相减得到的月数 ,就以为得到两日期之间的月数,事实证明有问题. 如2 ...

  9. js实现获取两个日期之间所有月份

    js实现获取两个日期之间所有月份 根据所选择的开始日期与结束日期获取之间的月份 代码如下 注意 根据所选择的开始日期与结束日期获取之间的月份 在使用过程中你可能需要获取两个时间的月份 组件moment ...

最新文章

  1. boost::sort模块实现spreadsort 键和数据排序示例
  2. php引用下级目录文件夹,使用PHP遍历文件夹与子目录的函数代码
  3. pip镜像源+修改linux配置用永久生效
  4. 不懂别瞎搞!Redis 性能优化的 13 条军规!
  5. 1-4 多文档界面处理(2)
  6. [洛谷P1908] 逆序对|归并排序|树状数组
  7. C++基础总结(4)-----指针
  8. 【渝粤教育】国家开放大学2018年春季 8668-22T汽车涂装技术(A) 参考试题
  9. 低压电力线载波通信原理
  10. signature=9b2caa13f2468eba05d2d57d9a88606d,【音响聚焦】顶级Hi-End音响发烧器材介绍(功放篇)...
  11. 排序算法——鸡尾酒排序
  12. 闲鱼搜索召回升级:向量召回个性化召回
  13. ps设计精讲精练笔记
  14. (二)立创EDA之新建工程,原理图,PCB
  15. 一个作业题---用python创建一个通讯录
  16. ArcGIS学习笔记-1.功能-1.4 矢量图基本
  17. kubernetes入门(上)
  18. 【Ubuntu】服务器使用
  19. 安科瑞:列头柜、监控系统、触摸屏的数据中心机房配电方案
  20. Selenium自动化测试入门(基于Python)

热门文章

  1. 腾讯云服务器快速创建一个表白网站。简单可操作。
  2. Timer+TimerTask实现数字时钟
  3. linux 文件系统 vfs,Linux 万物皆文件—VFS文件系统
  4. 计算机现在那个专业更好
  5. java 交集_Java大集合求交集的方法比较
  6. 计算机病毒不可能侵入rom,【常见问题】计算机病毒不可能侵入
  7. 【番杰的小技巧笔记】如何通过嘉立创免费打印立创EDA设计的PCB
  8. android app在腾讯开放平台认领应用给空白包签名
  9. 利用matplotlib绘制条形图,直观呈现2019年电影票房数据
  10. 服务器r730系统备份软件,r730服务器