如何查找两个日期/ DateTimePicker之间的天数

时间:2013-09-26 00:41:25

标签: .net vb.net datetime

在我的下面的代码中,如何设置到达日期和降级,以便我可以获得到期金额?我需要设置它们,因为我住在酒店的日子,所以我可以拿出一个总数。如果这有道理吗?我在Visual Basic中使用datetimepickers。

Public Class RentalForm
    'declare constants
    Const tax_rate_decimal As Decimal = 12.25D
    Const king_price_decimal As Decimal = 110.9D
    Const queen_price_decimal As Decimal = 105.9D
    Const double_price_decimal As Decimal = 95.9D

    'declare variables
    Private roomchargesumdecimal, taxamountsumdecimal, amountduesumdecimal As Decimal

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxnameofguest.TextChanged

    End Sub

    Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        'dimension local variables
        Dim numberofguestsinteger As Integer
        Dim roomchargedecimal, taxamountdecimal, amountduedecimal, taxratedecimal As Integer
        Dim arrivaldate, departuredate As Date

        Try
            'dates
            arrivaldate = Now

            'convert quantity to numeric
            numberofguestsinteger = Integer.Parse(TextBoxNumberofguests.Text)

            'calculate values for single person
            roomchargedecimal = numberofguestsinteger * (arrivaldate + departuredate)
            taxratedecimal = roomchargedecimal * tax_rate_decimal   

        Catch ex As Exception

        End Try

    End Sub

    Private Sub DateTimePickerarrivaldate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerarrivaldate.ValueChanged

    End Sub

    Private Sub Label16averagelengthofstay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label16averagelengthofstay.Click


    End Sub

    Private Sub RentalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class              

3 个答案:

答案 0 :(得分:16)

DateTime数学起初可能会令人困惑。但是,如果它们是DateTimePicker控件或变量并不重要,因为myDateTimePicker.Value DateTime类型。因此,您可以混合和匹配变量和控件,例如Arrival as Now和Departure from picker,并使用减法:

Dim arrivaldate As DateTime = DateTime.Now
Dim departuredate As DateTime = Me.DeparturePicker.Value

Dim DaysStayed as Int32 = departuredate.Subtract(arrivaldate).Days

要记住的是结果是TimeSpan对象。如果您查看结构,您会看到它提供从DaysTicks的单位所用的时间。

上面的代码从Days中获取TimeSpan值而不创建临时TimeSpan var。另一种方式:

Dim tsHotelStay = detarturedate.Value - arrivalDate
wholeDays = tsHotelStay.Days              ' e.g 7
totalDays = tsHotelStay.TotalDays         . e.g. 7.53
totalHrs = tsHotelStay.TotalHours         . eg 180.397

这一次,代码 创建了一个TimeSpan变量(tsHotelDay)。请注意,所有属性都以整数和小数形式提供(Ticks除外)。

最后,显示的2个减法方法(DateTime.Subtract(dt)myTs = dtA - dtB)功能相同:都返回TimeSpan对象。

答案 1 :(得分:2)

DateTimePicker对象有一个Value property,它会为每个对象提供一个DateTime。您可以通过从另一个中减去一个来获得两个DateTime对象的差异。

P.S。不要吃那样的所有例外。这是不酷的。

答案 2 :(得分:2)

也许你可以使用

`DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value)

计算两个日期之间的日期间隔(天)数。