如何计算两个日期之间相差的月数在.NET中?
- 内容介绍
- 文章标签
- 相关推荐
本文共计196个文字,预计阅读时间需要1分钟。
vbSub CalculateMonthsBetweenDates() Dim Date1 As New DateTime(2010, 5, 6) Dim Date2 As New DateTime(2009, 10, 12) Dim NumOfMonths As Integer=0
NumOfMonths=(Date2.Year - Date1.Year) * 12 + Date2.Month - Date1.Month Console.WriteLine(两个日期之间有 & NumOfMonths & 个月。)End Sub
我有以下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)
本文共计196个文字,预计阅读时间需要1分钟。
vbSub CalculateMonthsBetweenDates() Dim Date1 As New DateTime(2010, 5, 6) Dim Date2 As New DateTime(2009, 10, 12) Dim NumOfMonths As Integer=0
NumOfMonths=(Date2.Year - Date1.Year) * 12 + Date2.Month - Date1.Month Console.WriteLine(两个日期之间有 & NumOfMonths & 个月。)End Sub
我有以下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)

