Vb.net 获取两个日期时间之间的月数

Vb.net 获取两个日期时间之间的月数,vb.net,Vb.net,给出mnth=2。但是当我们观察时,这些日期之间只有32天。我希望得到一个结果,因为这两天之间只有32天 请帮忙。 在我的情况下,我可以考虑一个15天为一个月,但如果小于15,就不应该考虑。 < P>计算月份的差异,而不考虑日期的日成分。< /P> 例如,2012年8月31日和2012年9月1日之间的月差为1,尽管两个日期之间只有一天 如果你想考虑一天的组成部分,你必须在几天内而不是几个月来计算日期差,并计算你想要多少个月。 < P>获取完整月份的数量,你可以根据你的解释,做不同的事情, mnt

给出
mnth=2
。但是当我们观察时,这些日期之间只有32天。我希望得到一个结果,因为这两天之间只有32天

请帮忙。


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

< P>计算月份的差异,而不考虑日期的日成分。< /P> 例如,
2012年8月31日
2012年9月1日
之间的月差为1,尽管两个日期之间只有一天


如果你想考虑一天的组成部分,你必须在几天内而不是几个月来计算日期差,并计算你想要多少个月。

< P>获取完整月份的数量,你可以根据你的解释,做不同的事情,

mnth = DateDiff(DateInterval.Month, 8/30/2012, 10/1/2012)  
使用此功能,2011年5月31日(日/月/年)至2011年6月30日之间的完整月数为0,但2011年6月30日至2011年7月31日之间的完整月数为1。这可能不是你所期望的


使用此函数,将取日/日/月的比率,以便评估两个月的相对完成情况


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

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

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

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

如果您想要完整月份的数字,那么您需要比较从月份开始的日期

DateDifference diff = new DateDifference(date1, date2);
int months = (diff.Years*12) + diff.Months + diff.Days > 15 ? 1 : 0;
Sub-Main()
'mnth=DateDiff(DateInterval.Month,2012年8月30日,2012年10月1日)
Console.WriteLine(GetCompleteMonthCount(新的日期时间(2012,8,30)),新的日期时间(2012,10,1)))
Console.ReadLine()
端接头
作为整数的公共函数GetCompleteMonthCount(ByVal d1作为DateTime,ByVal d2作为DateTime)
如果d1.第1天,则
d1=d1。加月份(1)
d1=新日期时间(d1.年,d1.月,1)
如果结束
如果d2.第1天,则
d2=新日期时间(d2.年,d2.月,1)
如果结束
返回日期差异(DateInterval.Month,d1,d2)
端函数

上述解决方案都很好,但可能有点过于复杂,如何

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
函数wholeMonthsEd(d1,d2)为整数
'确定DateDiff函数输出
'这就产生了日历月的差异
initMonths=DateDiff(“m”,d1,d2)
'在要扣除/不是日历月的月份的当天进行计算
如果第(d2)天<第(d1)天,则
initMonths=initMonths-1
如果结束
整月=整月
端函数


为什么?32天是1个月外加一天。这并不是假设你不在乎那额外的一个月。那我该怎么办??我需要在此处获取完整月份数..:(你应该做的是给出一个你期望你的函数返回的不是琐碎的东西的说明。例如,大多数人会同意4月1日至5月1日或5月31日至6月30日都是一个月。但是4月30日至5月30日或4月15日至5月15日是什么?完整月的定义是什么?30天还是31天(更不用说28/29天了)?这是一个固定常数,或者应该根据开始/结束月份而变化?@Hybridzz,如果你制定了一个逻辑规范,它可以写出来。相应地编辑问题。哦,好的……(那么你能给出一个可以应用的精确解吗……?谢谢你。我现在就试试。)@Hybridzz,应该可以。@Hybridzz,这当然取决于你的期望。好吧。对我来说,一个月可能需要15天以上的时间。我当然感谢你的努力。空检查不会增加太多价值。
DateTime
是一种值类型,不能为空。这实际上并没有回答问题,只是尝试重新创建功能这出现在
DateTime.DaysInMonth
@Jodrell中,它确实回答了这个问题……但我同意你的观点,代码的某些部分可以使用dayinmonth。使用它并将结果与其他方法进行比较。好吧,这对这个问题来说太过分了……它在更精确的场景中更有用。
strAge
不是一个完整的数字e个月。你可以说这是一个比一个简单的数字更合理的答案,但是
TimeSpan
更好。你有一个更明确的用法…我认为它回答了用户的要求。;)1970年1月1日到1970年1月31日是一个完整的月吗?这完全取决于你的期望。这不太好,请检查
Public Function CompleteMonthsBetweenD( _
        ByVal start As DateTime, _
        ByVal end As DateTime) As Integer

    Return end.Subtract(start).TotalDays \ 30.436875
End Function
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;
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
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