在下面的代码中,如何设置到达日期和取消日期,以便可以得到应付金额?我需要将它们设置为在酒店住宿的天数,这样我才能得出总计。这有意义吗?我在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
最佳答案
DateTime
数学一开始可能会令人困惑。但是,它们是否是DateTimePicker
控件或变量并不重要,因为myDateTimePicker.Value
是是DateTime
类型。因此,您可以混合和匹配变量和控件,例如“从选择器中到达”和“从选择器中离开”,只需使用减法即可:
Dim arrivaldate As DateTime = DateTime.Now
Dim departuredate As DateTime = Me.DeparturePicker.Value
Dim DaysStayed as Int32 = departuredate.Subtract(arrivaldate).Days
要记住的是,结果是一个TimeSpan对象。如果看一下结构,您会看到它提供了从
Days
到Ticks
的单位耗时。上面的代码从
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对象。关于.net - 如何找到两个日期之间的天数/DateTimePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19017466/