获取两个DateTime之间的月数

时间:2012-10-02 13:12:22

标签: vb.net

mnth = DateDiff(DateInterval.Month, 8/30/2012, 10/1/2012)  

提供 mnth = 2。但是,当我们查看时,这些日期之间只有32天。我期待结果mnth=1 ,因为这些天之间只有32天

请帮助..

在我的情景中,我可以考虑将15个月作为一个月,但如果它少于15天,则不应该考虑。

5 个答案:

答案 0 :(得分:4)

要获得完整月份的数量,您可以根据您的解释做不同的事情,

Public Function CompleteMonthsBetweenA( _
        ByVal start As DateTime, _
        ByVal end As DateTime) As Integer

    Dim invertor = 1

    If (start > end) Then
       Dim tmp = end
       end = start
       start = tmp
       invertor = -1
    End If

    Dim diff = ((end.Year - start.Year) * 12) + end.Month - start.Month

    If start.Day > end.Day Then
        Return (diff - 1) * invertor
    Else
        Return diff * invertor
    End If
End Function

使用此功能,2011年5月31日(年/月/日)和30/06/2011之间的完整月份数为0,但在2011年6月30日至2011年7月31日之间是1。或者可能不是你所期望的。


Public Function CompleteMonthsBetweenB( _
        ByVal start As DateTime, _
        ByVal end As DateTime) As Integer

    Dim invertor = 1

    If (start > end) Then
       Dim tmp = end
       end = start
       start = tmp
       invertor = -1
    End If

    Dim diff = ((end.Year - start.Year) * 12) + end.Month - start.Month

    Dim startDaysInMonth = DateTime.DaysInMonth(start.Year, start.Month)
    Dim endDaysInMonth = DateTime.DaysInMonth(end.Year, end.Month)

    If (start.Day / startDaysInMonth) > (end.Day / endDaysInMonth) Then
        Return (diff - 1) * invertor
    Else
        Return diff * invertor
    End If
End Function

使用此功能,比率为Day / DaysInMonth,因此可以评估两个月的相对完成情况。


Public Function CompleteMonthsBetweenC( _
     ByVal start As DateTime, _
     ByVal enddate As DateTime) As Integer

    Dim invertor = 1

    If (start > enddate) Then
        Dim tmp = enddate
        enddate = start
        start = tmp
        invertor = -1
    End If

    Dim diff = ((enddate.Year - start.Year) * 12) + enddate.Month - start.Month

    Dim remainingDays = _
      (DateTime.DaysInMonth(start.Year, start.Month) - start.Day) + enddate.Day

    If remainingDays < 15 Then
        Return (diff - 1) * invertor
    Else
        Return diff * invertor
    End If
End Function

如果剩余天数小于幻数15,则此功能仅向下舍入,我认为这是您在更新中要求的。


Public Function CompleteMonthsBetweenD( _
        ByVal start As DateTime, _
        ByVal end As DateTime) As Integer

    Return end.Subtract(start).TotalDays \ 30.436875
End Function

此功能采用更简单的方法将总天数除以公历中每月的平均天数。

答案 1 :(得分:2)

计算月份差异时不考虑日期的日期部分。

例如,8/31/20129/1/2012之间的差异为1,尽管日期之间只有一天。

如果你想考虑日期组件,你必须以天而不是几个月来获得日期差异,并计算你想要的月数。

答案 2 :(得分:1)

这是我使用的类(它是C#,但很容易转换为VB.NET)。 它可以使用数年,数月,数天...它非常适合以#Y-#M-#D格式显示年龄。

public class DateDifference
    {
        /// <summary>
        /// defining Number of days in month; index 0=> january and 11=> December
        /// february contain either 28 or 29 days, that's why here value is -1
        /// which wil be calculate later.
        /// </summary>
        private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    /// <summary>
    /// contain from date
    /// </summary>
    private DateTime fromDate;

    /// <summary>
    /// contain To Date
    /// </summary>
    private DateTime toDate;

    /// <summary>
    /// this three variable for output representation..
    /// </summary>
    private int year;
    private int month;
    private int day;

    public DateDifference(DateTime d1, DateTime d2)
    {
        int increment;

        if (d1 > d2)
        {
            this.fromDate = d2;
            this.toDate = d1;
        }
        else
        {
            this.fromDate = d1;
            this.toDate = d2;
        }
        /// 
        /// Day Calculation
        /// 
        increment = 0;

        if (this.fromDate.Day > this.toDate.Day)
        {
            increment = this.monthDay[this.fromDate.Month - 1];

        }
        /// if it is february month
        /// if it's to day is less then from day
        if (increment == -1)
        {
            if (DateTime.IsLeapYear(this.fromDate.Year))
            {
                // leap year february contain 29 days
                increment = 29;
            }
            else
            {
                increment = 28;
            }
        }
        if (increment != 0)
        {
            day = (this.toDate.Day + increment) - this.fromDate.Day;
            increment = 1;
        }
        else
        {
            day = this.toDate.Day - this.fromDate.Day;
        }

        ///
        ///month calculation
        ///
        if ((this.fromDate.Month + increment) > this.toDate.Month)
        {
            this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment);
            increment = 1;
        }
        else
        {
            this.month = (this.toDate.Month) - (this.fromDate.Month + increment);
            increment = 0;
        }

        ///
        /// year calculation
        ///
        this.year = this.toDate.Year - (this.fromDate.Year + increment);

    }

    public override string ToString()
    {
        //return base.ToString();
        return this.year + " Year(s), " + this.month + " month(s), " + this.day + " day(s)";
    }

    public int Years
    {
        get
        {
            return this.year;
        }
    }

    public int Months
    {
        get
        {
            return this.month;
        }
    }

    public int Days
    {
        get
        {
            return this.day;
        }
    }
}

<强> USAGE:

DateDifference diff = new DateDifference(date1, date2);
int months = (diff.Years*12) + diff.Months + diff.Days > 15 ? 1 : 0;

答案 3 :(得分:0)

如果您想要完整的月份数,那么您需要比较在壁橱月初开始的日期。

Sub Main()

    ' mnth = DateDiff(DateInterval.Month, 8/30/2012, 10/1/2012)  

    Console.WriteLine(GetCompleteMonthCount(New DateTime(2012, 8, 30), New DateTime(2012, 10, 1)))
    Console.ReadLine()

End Sub


Public Function GetCompleteMonthCount(ByVal d1 As DateTime, ByVal d2 As DateTime) As Integer

    If d1.Day <> 1 Then
        d1 = d1.AddMonths(1)
        d1 = New DateTime(d1.Year, d1.Month, 1)
    End If

    If d2.Day <> 1 Then
        d2 = New DateTime(d2.Year, d2.Month, 1)
    End If

    Return DateDiff(DateInterval.Month, d1, d2)
End Function

答案 4 :(得分:-1)

上面的解决方案都非常好,但是可能有些复杂,

Function wholeMonthsEd(d1, d2) As Integer

    ' determine the DateDiff function output
    ' which gives calendar month difference
    initMonths = DateDiff("m", d1, d2)

    ' do calcs on the Day of the month to deduct/not a calendar month
    If Day(d2) < Day(d1) Then
        initMonths = initMonths - 1
    End If

    wholeMonths = initMonths

End Function