.net - 计算两个日期之间的月数

标签 .net vb.net datetime date

我有以下 VB.NET 代码:

Dim Date1 As New DateTime(2010,5,6)
Dim Date2 As New DateTime(2009,10,12)
Dim NumOfMonths = 0 ' This is where I am stumped

我想要做的是找出两个日期之间有多少个月。任何帮助,将不胜感激。

最佳答案

这是您可以使用的方法:

Public Shared Function MonthDifference(ByVal first As DateTime, ByVal second As DateTime) As Integer
    Return Math.Abs((first.Month - second.Month) + 12 * (first.Year - second.Year))
End Function

像这样:
Dim Date1 As New DateTime(2010,5,6)
Dim Date2 As New DateTime(2009,10,12)
Dim NumOfMonths = MonthDifference(Date1, Date2)

关于.net - 计算两个日期之间的月数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3249968/

相关文章:

c# - 重用 keep-alive HTTP web 请求对象

.net - 在 VB.NET 中使用 LINQ 的 ForEach 和匿名方法

mysql - 从一个源绑定(bind)多个下拉列表

jquery - 自定义 jquery 验证器无法在 iPad 上运行

c# - 在 .NET 中生成、显示和打印简单文档

c# - 日期时间格式 yyyy-MM-ddT00 :00:00

c# - 在 .net ReportViewer 控件中查看 MS Access Report

asp.net - 如何传递单选按钮值来过滤数据

JavaScript - 获取两个日期之间的分钟数

android - 如何检查 "Automatic date and time"是否启用?