本文介绍了计算两个日期之间的月数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下VB.NET代码:
I have the following VB.NET Code:
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我要做的是找出两个日期之间有多少个月。任何帮助将不胜感激。
What I am trying to do is find out how many months are between the 2 dates. Any help would be appreciated.
推荐答案以下是您可以使用的方法:
Here's a method you could use:
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)更多推荐
计算两个日期之间的月数
发布评论