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

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

请帮忙..

在我的情况下,我可以将 15 天以上视为一个月,但如果小于 15 天,则不应考虑。

4

5 回答 5

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 日 (dd/mm/yy) 和 2011 年 6 月 30 日之间的完整月数为 0,但 2011 年 6 月 6 日至 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

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

于 2012-10-02T13:22:45.507 回答
2

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

例如,8/31/2012和之间的月差9/1/2012为 1,尽管这两个日期之间只有一天。

如果您想考虑日组件,您必须以天而不是月为单位获取日期差异,并计算您想要的月数。

于 2012-10-02T13:18:10.690 回答
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;
        }
    }
}

用法:

DateDifference diff = new DateDifference(date1, date2);
int months = (diff.Years*12) + diff.Months + diff.Days > 15 ? 1 : 0;
于 2012-10-02T15:10:38.820 回答
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
于 2012-10-02T15:25:31.717 回答
-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
于 2020-04-02T11:31:11.070 回答